I too had this issue until I found out I was going about it the wrong
way.  See Django docs -
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form


Your code should be :-

formContent = PropertyForm(request.POST)
form_instance = formContent.save(commit = False)
form_instance.user = request.user
form_instance.save()



On Apr 8, 4:03 am, ramya <mail.ra...@gmail.com> wrote:
> class Property(models.Model):
>   property_id = models.AutoField(primary_key = True)
>   property_name = models.CharField("Name", max_length = 30)
>   user = models.ForeignKey(User)
>
> class PropertyForm(ModelForm):
>   class Meta:
>     model = Property
>     exclude = ('user')
>
> def addProperty(request):
>   formContent = PropertyForm(request.POST)
>   formContent.save(commit = False)
>   formContent.user = request.user
>   formContent.save()
>
> create table property (     property_id int(11) NOT NULL
> auto_increment,     property_name varchar(30) NOT NULL,  primary key
> (property_id),     foreign key(user_id) references auth_user(id) );
>
> I am trying to save a record into property model with the current user
> id (of logged in user).
>
> I do not want to query user auth_user table(unnecessary query)
> a. if the field name in db is user_id
> This throws the error
> IntegrityError at /addProperty
> (1048, "Column 'user_id' cannot be null")
>
> b. if the field name is user
> Throws an sql error
> OperationalError at /addProperty
> (1054, "Unknown column 'user_id' in 'field list'")
>
> Any pointers.
> Thanks and Regards,
> Ramya Krishnan

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