Junhao
Junhao

Reputation: 179

Updating inherited attributes in django model objects

I have troubles updating attributes that are inherited from other tables

class AgentCategory(models.Model):
""" Agent Category """
    class Meta:
    verbose_name_plural = "agentcategories"
    name = models.CharField(max_length=200, unique=True)
    description = models.TextField(blank=True)

class Agent(models.Model):
    agentcategory = models.ManyToManyField(AgentCategory,null=True)

How should i go about manually updating agentcategory in Agent model? As of now i am trying out this method, however, it does not work):

property_selected.agentcategory = "api/v1/agentcategory/3"
property_selected.save()

Any ideas? Thanks!

Upvotes: 0

Views: 43

Answers (1)

Ahsan
Ahsan

Reputation: 11832

As Agent has ManyToManyField relation with AgentCategory.

agentcategory would contain the list of entries.

you can update its entries by,

agent_cats = AgentCategory.objects.filter(...)
property_selected.agentcategory.clear()
property_selected.agentcategory = agent_cats
property_selected.agentcategory.save()

Upvotes: 1

Related Questions