python 2.7 - Can't add object to Django's Many-To-Many Field -
i'm trying make app many-to-many field. , write , wont try it. so, started shell , make objects , error.
>>> mzz.controlsorganization.add(org1, org2) traceback (most recent call last): file "<console>", line 1, in <module> file "/home/fdobrovolny/virtualenv/first/lib/python2.7/site-packages/django/db/models/fields/related.py", line 848, in __get__ through=self.field.rel.through, file "/home/fdobrovolny/virtualenv/first/lib/python2.7/site-packages/django/db/models/fields/related.py", line 538, in __init__ (instance, source_field_name)) valueerror: "<mzz: test 1>" needs have value field "mzz" before many-to-many relationship can used.
mzz class:
class mzz(models.model): name = models.charfield(max_length=100) name.short_decription = u'název mzz' ident = models.charfield(max_length=45, unique=true) active = models.booleanfield() active.boolean = true kind = models.foreignkey(kind) deliverydate = models.datefield() stateafterdelivery = models.charfield(max_length=200) dateofcommissioning = models.datefield() prescribedparameters = models.charfield(max_length=200) responsiblestaff = models.foreignkey(user) dateofmanufacture = models.datefield() manufacturer = models.foreignkey(organization, related_name='manufacturer') type = models.charfield(max_length=50) serialnumber = models.charfield(max_length=80) frequencyofcontrols = models.foreignkey(controls_frequency) location = models.charfield(max_length=50) methodofcontrols = models.charfield(max_length=100) controlsorganization = models.manytomanyfield(organization, related_name='controlsorganization') servisorganization = models.manytomanyfield(organization, related_name='servisorganization') def __unicode__(self): return self.name'
organization class:
class organization(models.model): name = models.charfield(max_length=200) adress = models.charfield(max_length=200) telephonenumber = models.charfield(max_length=35) email = models.emailfield() def __unicode__(self): return self.name
can please me?
you have create mzz object , save() first , add organization.
mzz = mzz() # create mzz.save() # save() o = organization() o.save() m.organization.add(o) # add(o)
https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/
Comments
Post a Comment