0

When running a Django model create call I get an error message as follows:

TypeError: 'keywords' is an invalid keyword argument for this function

The create object is called form within a ModelViewset and looks like this:

the_item = Item.objects.create(url=the_photo_url, owner=the_user, keywords=the_keywords, item_type=the_item_type, title=the_title)

My Item model looks like this (it has a keywords field that links to a keyword table):

class Item(models.Model):

    ITEM_TYPES = (
        ('V', 'Vine'),
        ('Y', 'YouTube'),
        ('P', 'Photo'),         # Photo is stored by us on a CDN somewhere
        ('F', 'Flickr'),
        ('I', 'Instagram'),
        ('D', 'DeviantArt'),
        ('5', '500px'),
    )
    owner = models.ForeignKey(User, on_delete=models.CASCADE)     
    title = models.CharField(max_length=60, default='')           
    url = models.CharField(max_length=250, default='', unique=True)
    item_type = models.CharField(max_length=1, choices=ITEM_TYPES)    
    keywords = models.ManyToManyField(Keyword, related_name='keywords')
    credits_applied = models.IntegerField(default=5000)
    credits_left = models.IntegerField(default=10)
    credits_gifted = models.IntegerField(default=0)
    date_added = models.DateTimeField(auto_now_add=True)               
    liked = models.IntegerField(default=0)                        
    disliked = models.IntegerField(default=0)                        
    active = models.BooleanField(default=True)
    comment = models.CharField(max_length=100, blank=True)         
    approved = models.BooleanField(default=0)                      

The related Keyword model looks like this:

class Keyword(models.Model):
    name = models.CharField(max_length=30)

The the_keyords argument to the Item.objects.create() is a Python list: [1,21]

Why is Django complaining about the Keywords parameter?

6
  • Possible duplicate of When creating a model instance how to fill ManyToMany field? May 5, 2016 at 18:48
  • Unfortunately I don't understand the answer in the link you give.
    – Bill Noble
    May 5, 2016 at 18:51
  • Are [1, 21] id's of Keyword instances? May 5, 2016 at 18:53
  • I find manytomany hard to understand. In the Item model there is a keywords field with a manytomany relationship to a keyword model that contains a list of string keywords. The [1,21] are indexes into the keyword table.
    – Bill Noble
    May 5, 2016 at 18:57
  • Understand that keywords is not a db column in the Item table. For many-to-many fields, django creates a through model with two foreign keys to the two models that are to be related by the many-to-many. In that sense, these fields are special. May 5, 2016 at 18:59

1 Answer 1

3

You cannot use a ManyToMany field like that in the creation of a Model instance. For the creation of the necessary through model instances, the object must be saved first (to have a pk). So create the instance first, and then use the add method to add Keyword instances (you can do so with multiple at a time):

the_item = Item.objects.create(...)  

keywords = list(Keyword.objects.filter(id__in=[1, 21]))
the_item.keywords.add(*keywords)

Or, maybe a little more performant, using the through model and bulk_create directly will reduce the entire adding to one db hit:

kw_ids = [1, 21]
through_model = Item.keywords.through
through_model.bulk_create(
    [through_model(item_id=the_item.pk, keyword_id=kw_id) for kw_id in kw_ids]
)
1
  • That is brilliant! Thanks. I am not sure I would ever have worked that out!
    – Bill Noble
    May 5, 2016 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.