Oh and just to re-state, this exact same code works like a champ on import of School or Parent data (neither of which have a ManyToMany field).
Keyton On Jan 20, 11:32 pm, Keyton Weissinger <key...@gmail.com> wrote: > Hi Malcolm, > > Thanks very much for the quick reply. I'm using this code as part of a > view in which I'm processing an import of data (part of my reusable > django-batchimport app on google code). Here's a dpaste of the main > part of the code:http://dpaste.com/111306/ > > "model_import_info.model_for_import" (from the code) returns the > appropriate model. I've confirmed that it is returning a Student model > in this case (as the call to get at line #3 works just fine). I *am* > reusing this model over and over, btw. However, I have confirmed that > even if I create it anew each time, I get the same behavior. Also, > none of the data I am using actually has a Parent in it (the > ManyToMany field culprit). > > To your point, I've tried it both with explicitly setting the model to > Student and using get_model. Using both methods, it works at > interactive prompt but not in the view. > > Here's a dpaste of the exact stack trace I'm getting:http://dpaste.com/111304/ > > Not to confuse the issue, but looking at the django code from the > dpaste -- at the "setattr(self, field.attname, val)" call (line 38 on > dpaste link) -- the local variables tell me I'm dealing with a > ManyToMany object (<django.db.models.fields.related.ManyToManyField > object at 0x2e04c90>). I say that just to rule out some problem with > the School object (a OneToMany) that *IS* being passed in. > > I appreciate any time you have to spend on it very much. > > Best, > Keyton > > On Jan 20, 11:02 pm, Malcolm Tredinnick <malc...@pointy-stick.com> > wrote: > > > 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 -~----------~----~----~----~------~----~------~--~---