django - ValueError: "<MODEL MODELOBJECT>" needs to have a value for field "Mrefs" before this many-to-many relationship can be used -
i getting following error when trying add many many relationship. error happens when trying add many many field. same model no data many-many field loads fine.
traceback (most recent call last): file "manage.py", line 10, in <module> execute_from_command_line(sys.argv) file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv) file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__) file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute output = self.handle(*args, **options) file "/home/app/pycharmprojects/app/management/commands/import_data_csv.py", line 101, in handle manualref.manual_reference = manual.objects.get(mtype=mid) file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1188, in __set__ manager = self.__get__(instance) file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1178, in __get__ through=self.field.rel.through, file "/home/app/.virtualenvs/app/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 831, in __init__ (instance, source_field_name)) valueerror: "<model modelobject>" needs have value field "manual_reference" before many-to-many relationship can used
my models:
from django.db import models
class manualrefs(models.model): reference = models.textfield() reference_type = models.charfield(max_length=10, blank=true) manual_reference = models.manytomanyfield('manual', blank=true, null=true) class manual(models.model): info = models.foreignkey('tdictionary', blank=true, null=true) mol = models.foreignkey('mdictionary', blank=false, null=false) mtype = models.foreignkey('mtype', blank=true, null=true) def __unicode__(self): if self.mtype: str = '-' seq = (self.mtype.description, self.info.name, self.mol.mol) return str.join(seq) else: return self.pk
my code , here doing :
mid = mtype.objects.get(description =row[16]) manref = manualrefs() man = manual() refer in references: types, reference = refer.split(':', 1) manref.reference_type = types manref.reference = reference manref.manual_reference = manual.objects.get(mtype=mid) manref.save() man.save() manref.manual.add(manref)
what doing wrong?thanks
in order access manytomany
field, object must saved database first. means can manref.manual_reference = manual.objects.get(mtype=mid)
after manref.save()
.
Comments
Post a Comment