On Tue, 2009-01-20 at 19:32 -0800, Keyton Weissinger wrote: > Hello, > > OK. I'm totally stuck. I am trying to create an object (of type > Student -- see below) with the following: > > new_student = Student.objects.create(**import_object_dict) > > I'm using the following dictionary as "import_object_dict" above > (aside from the name of the school, this is fake data, so no privacy > worries): > > import_object_dict={'address': u'142, Quilly Lane', 'city': > u'Columbus', 'dob': '1956-12-29', 'email': > u'miles.l.ye...@spambob.com', 'first_name': u'Yeung', 'phone_primary': > u'614-468-5940', 'school': <School: ARBOR MONTESSORI (DECATUR, GA)>, > 'state': u'OH', 'title': u'Mr.', 'type': u'Student', 'zip': 43215.0} > > Note that I am NOT yet trying to SAVE the Student object, just > instantiate it. > > But every time I try to instantiate this new Student, I get the > following error: > 'Student' instance needs to have a primary key value before a many-to- > many relationship can be used. > > The Student object DOES have a ManyToMany relationship (with Parent -- > see below) but I'm not in any way trying to access it here. > > I am using inheritance which may be complicating matters (see models > below). > > And here's a clue that may help you (but hasn't yet helped me): > > When I try this on the command line (using "manage.py shell" and then > importing Student, creating a School object, etc) it works just fine. > No error. Using the exact same dictionary as above the exact same way. > > Also, the code above isn't exactly accurate. I'm actually dynamically > getting the model object using get_model. I simplify the question here > because I've used the same exact method to create other types of > objects (without ManyToMany fields) with no problem whatsoever.
Bonus points for a clear problem description, so thanks for doing that. Nothing jumps out at me as obviously wrong, unfortunately. You don't mention how you're using this when it fails -- is it part of a script or a view or what? In any case, I would try removing the call to get_model(). Even if you just test that the model name is what you think should return the Student model and then explicitly set the class you're creating to be Student, instead of whatever get_model() returns. I *hope* that won't make a difference, because get_model() and friends give me a nose-bleed (and James Bennett gets far too much enjoyment out of watching me fix those bugs), but it would be nice to know one way or the other. Are you using get_model() at the interactive prompt? Does it work there instead of failing? I'll try to make some time later on today to experiment with the code you've given here and see if I can repeat it (in the middle of something right now and just rest my brain by answering email, so not able to do it this minute, sorry). Right now, though, it's not obvious what you're doing that is strange. Which is a shame. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---