Reputation: 33655
I have the following models:
class Catalogue(Base):
name = models.CharField(max_length=60)
params = models.ForeignKey("Params", related_name="params_item", null=True, blank=True)
class Params(Base):
name = models.CharField(max_length=60)
But when I do this:
Params.objects.create(params_item=object_cat, name="Test")
I get the error:
params_item' is an invalid keyword argument for this function
Why can I not use the reverse relation name to set this?
Upvotes: 0
Views: 144
Reputation: 2165
You can't do it this way, that's not how you use a reverse relation. You have to do it this way instead
...
param = Params(name='Some Param')
param.save()
catalog = Catalog(name='Some Catalog')
catalog.params = param
catalog.save()
Then you'd use the reverse relation to query the list of catalogs related to it like so
catalogs = param.params_item.all()
Upvotes: 2
Reputation: 309109
The reverse relation can be used to query related items, but you can't use it to set the object as you are trying.
You need to create the params, then update the related object.
params = Params.objects.create(name="Test")
object_cat.params = params
object_cat.save()
Upvotes: 1