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.

Any suggestions would be VERY much appreciated.

Thank you.

Keyton


class Person(models.Model):
    title = models.CharField("Title", max_length=4,
choices=TITLE_CHOICES)
    first_name = models.CharField("First name", max_length=100)
    last_name = models.CharField("Last name", max_length=100)
    address = models.CharField("Home street address", max_length=100)
    city = models.CharField("City", max_length=100)
    state = models.CharField("State", choices=STATE_CHOICES,
max_length=100)
    zip = models.CharField("Zipcode", max_length=100)
    email = models.EmailField("Preferred email address")
    phone_primary = PhoneNumberField("Primary phone #")
    phone_secondary = PhoneNumberField("Secondary phone #",
blank=True, null=True)
    invite_key = models.CharField("Last invitation code",
max_length=50, blank=True, null=True)
    invite_datetime = models.DateTimeField("Last invitation
date",blank=True, null=True)
    type = models.CharField("User type", max_length=15,
choices=USER_TYPE_CHOICES, blank=True, null=True)
    school = models.ForeignKey(School, blank=True, null=True)
    user = models.ForeignKey(User, unique=True, blank=True, null=True)

    def __unicode__(self):
        return self.last_name + ', ' + self.first_name

    def get_absolute_url(self):
        return ('profiles_profile_detail', (), { 'username':
self.user.username })
    get_absolute_url = models.permalink(get_absolute_url)

    def save(self, force_insert=False, force_update=False):
        if self.user:
            self.user.email = self.email
        super(Person, self).save(force_insert, force_update) # Call
the "real" save() method.

class Parent(Person):
    pass

class Student(Person):
    dob = models.DateField("Student's birth date", help_text='Used for
determining age.')
    parents = models.ManyToManyField(Parent,
related_name="student_parent",blank=True, null=True, )

    def __unicode__(self):
        return self.last_name


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to