tags:

views:

47

answers:

3

I created a project with Django and am trying to write from forms to db. The model class has two classes :

class Contact(models.Model):
    name = models.CharField(max_length=200)
    birth_day = models.DateTimeField()
    address = models.CharField(max_length=200)

class PhoneNumber(models.Model):
    PHONETYPE_CHOICES = (
        (0, 'Home'),
        (1, 'Work'),
        (2, 'Fax'),
        (3, 'Mobile'),
        (4, 'Other'),
    )
    contact = models.ForeignKey(Contact)
    phone_type = models.CharField(max_length=255, choices=PHONETYPE_CHOICES)
    phonenumber = models.CharField(max_length=30) 

Now if I want to write to this with forms and I use only:

Name

Birthday

Address

Number type

Phone Number

as form fields.

I get:

IntegrityError x_phonenumber.contact_id may not be NULL

This is part of the view:

def main(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name'],
            birth_day = form.cleaned_data['birth_day'],
            address = form.cleaned_data['address'],
#            contact1= form.cleaned_data['id']
            phone_type = form.cleaned_data['phone_type']
            phonenumber = form.cleaned_data['phonenumber']
            contact = Contact(
                name = form.cleaned_data['name'],
                birth_day = form.cleaned_data['birth_day'],
                address = form.cleaned_data['address'],
            )
            contact.save()
            number = PhoneNumber(
#                contact1 = form.cleaned_data ['id']
                phone_type = form.cleaned_data['phone_type'],
                phonenumber = form.cleaned_data['phonenumber'],

            )
            number.save()

I know I have to fill in the ID of the person in that ForeignKey, but I thought that is what the ForeignKey will do for me.

The two commented out object "contact1" did not work. But that is basically what I want, add the id to this.

Also, Django always adds an _id primary key to every table (contact and Phonenumber).

So i did not get why, Django did not add to this.

How can I save this to db with the correct id, primary key etc..

Thanks

A: 

How could Django know wich rows should it chain? You have to pass contact instance as argument for foreign key field.

contact = Contact(...)
contact.save()
number = PhoneNumber(...)
number.contact = contact # here comes the instance of Contact model
number.save()
Anpher
A: 

Consider these two lines:

number = PhoneNumber(
    #   contact1 = form.cleaned_data ['id']
    phone_type = form.cleaned_data['phone_type'],
    phonenumber = form.cleaned_data['phonenumber'],
)

number.save()

You are creating an instance of PhoneNumber and saving it. However any instance of PhoneNumber is required to have a valid foreign key to an instance of Contact. This is not being set before you save() the PhoneNumber and hence you get the error.

In order to solve this, point the contact field of the PhoneNumber instance to the Contact you saved before you save the phone number. Something like this:

number = PhoneNumber(
    #   contact1 = form.cleaned_data ['id']
    phone_type = form.cleaned_data['phone_type'],
    phonenumber = form.cleaned_data['phonenumber'],
)

number.contact = contact # <===== Attach the contact.
number.save()
Manoj Govindan
A: 

The ForeignKey can't magically know which person it's supposed to be pointing at, unless you tell it.

In your case, once you've done contact.save(), you now have a Contact instance, so you can use that.

number = PhoneNumber(
    contact = contact,
    phone_type = form.cleaned_data['phone_type'],
    phonenumber = form.cleaned_data['phonenumber'],
)
number.save()
Daniel Roseman
Thanks! That was exactly what I was looking for. Worked!!
MacPython