python - Django ManyToManyField not saving in admin -
models.py
class document(models.model): document_type_d = models.foreignkey(documenttype, verbose_name="document type") class section(models.model): document_type_s = models.manytomanyfield(documenttype, verbose_name="document type") class documenttype(models.model): document_type_dt = models.charfield("document type", max_length=240)
when create section in admin doesn't appear save document_type_s.
in django shell can see documenttype saving properly:
>>> dt = documenttype.objects.all() >>> d in dt: ... print d.document_type_dt ... campus lan wan unified communications
when check sections get:
>>> original_sections = section.objects.all() >>> in original_sections: ... print a.document_type_s ... lld.documenttype.none lld.documenttype.none lld.documenttype.none lld.documenttype.none lld.documenttype.none lld.documenttype.none lld.documenttype.none lld.documenttype.none
and when check documents:
>>> d = document.objects.all() >>> e in d: ... print e.document_type_d ... campus lan wan campus lan
and if try create new section:
>>> s = section(document_type_s="campus lan", name="fake", original=true) traceback (most recent call last): file "<console>", line 1, in <module> file "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 480, in __init__ raise typeerror("'%s' invalid keyword argument function" % list(kwargs)[0]) typeerror: 'document_type_s' invalid keyword argument function
django appears have created field in database, looking @ migrations file:
migrations.createmodel( name='section', fields=[ ('id', models.autofield(verbose_name='id', serialize=false, auto_created=true, primary_key=true)), ('document_type_s', models.manytomanyfield(to='lld.documenttype', verbose_name=b'document type')), ]
i don't know causing this?
how think going work?
section(document_type_s="campus lan", name="fake", original=true)
document_type_s
manytomany field passing in string value
read docs manytomany:
https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_many/
for many many relation created section
instance has exist (because in db m2m relation table 2 foreign keys)... need to:
dt = documenttype.objects.get(pk=1) section = section.objects.create(name="fake", original=true) section.document_type_s.add(dt)
Comments
Post a Comment