hi,

I need to create 2 objects, one containing a reference to the other,
in one shot. I get an IntegrityError exception while trying to do so,
claiming that the foreign key is None on creation. Hope someone can
help...
(when I refresh the browser and rePOST the data it works as the
Product object is already there and is not created at the same time)

Thanks, Yaniv

I have the following models:
Class Product
    productId   = models.AutoField(primary_key=True)
    name                = models.CharField(max_length=200,
unique=True)
    description    = models.CharField(max_length=200, blank=True)
Class Manufacturer
    manId            = models.AutoField(primary_key=True)
    product          = models.ForeignKey(Product)
    contact          = models.ForeignKey(User)
    price             = models.PositiveIntegerField()
I created a form containing fields for both, with a 'save' function:
class ProductForm(forms.Form):
    name = forms.CharField(max_length=200)
    description = forms.CharField(max_length=200)
    price = forms.IntegerField(min_value=0)
    def save(self):
          prod = Product(name=self.cleaned_data['name'],
                    description=self.cleaned_data['description'])
          prod.save()
          ma = Manufacturer(product=prod,
                  contact=self.data
['user'],                                  # this is being passed by
the view
                  price=self.cleaned_data['price'])
           ma.save();
The problem: the new product is being saved but on the
manufacturer.save() I get the following exception:
Exception Type:         IntegrityError
Exception Value:        (1048, "Column 'product_id' cannot be null")
Exception Location:     C:\Development\Python25\Lib\site-packages
\django
\db\backends\mysql\base.py in execute, line 88
Environment:
Request Method: POST
Request URL: http://localhost:8000/addproduct/
Django Version: 1.0.2 final
Python Version: 2.5.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.views.generic',
 'mysite.myapp',
 'registration']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')

Traceback:
File "C:\Development\Python25\Lib\site-packages\django\core\handlers
\base.py" in get_response
  86.                 response = callback(request, *callback_args,
**callback_kwargs)
File "C:\Development\Python25\Lib\site-packages\django\contrib\auth
\decorators.py" in __call__
  67.             return self.view_func(request, *args, **kwargs)
File "C:\Development\googleapp_projects\busa\..\mysite\myapp\views.py"
in registerProduct
  63.             f.save()
File "C:\Development\googleapp_projects\busa\..\mysite\myapp\forms.py"
in save
  76.         ma.save();
File "C:\Development\Python25\Lib\site-packages\django\db\models
\base.py" in save
  311.         self.save_base(force_insert=force_insert,
force_update=force_update)
--~--~---------~--~----~------------~-------~--~----~
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