Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Saving form with User as Foreign key

I have a problem and I'm looking for the answer like a week ago without finding any. I want to save a model, using the Django User as a foreign key, I tried creating a new user like this:

user=User.objects.create_user(username, email, password)

And then put the entire user or the user id and does not work, just like this:

studio_form.user_id=user
studio_form.user_id=user.id

And nothing happens, everything is saved in the database, the user, the group, but not the user as a foreign key, the foreign key user_id is null every single time, help please.

so, this is my code:

Django Model:

from django.contrib.auth.models import User
class Studios(models.Model):
    user_id=models.ForeignKey(User)
    studio_name=models.CharField(max_length=250)

Django Forms:

from django import forms
from web.models import *

class studioForm(forms.ModelForm):
    class Meta:
        model = Studios
        exclude=('user_id')
        widgets = {
            'password': forms.PasswordInput(),
        }

Django View:

def studio_register(request):
    if request.method == "POST":
        studio_form=studioForm(request.POST, request.FILES)
        if studio_form.is_valid():
            studio_form.save(commit=False)
            data=request.POST
            username=data.get("user_name")
            email=data.get("email")
            password=data.get("password")
            user=User.objects.create_user(username, email, password)
            group=Group.objects.get(name='studio')
            group.user_set.add(user)
            studio_form.user_id=user.id
            studio_form.save()

Any idea?

like image 243
Oscar Gallo Avatar asked Jul 17 '14 01:07

Oscar Gallo


1 Answers

You are passing your data back to the studio_form here:

def studio_register(request):
    # other view code
    studio_form.user_id=user.id
    studio_form.save()

You actually need to pass the data to the Studios model in order to save it to the database. The Form is only for cleaning data and temporary holding it until you access the model to save the data to the database.

So if here is you model:

from django.contrib.auth.models import User
class Studios(models.Model):
    user_id=models.ForeignKey(User)
    studio_name=models.CharField(max_length=250)

You need access it in your view:

def studio_register(request):
    # other view code
    studio = Studios.objects.create(user_id=user, studio_name=name)

That will create a Studios object, saving the data to the database.

user_id needs to be a User object (not the user.id) since it is a foreign key to the User model. So once you get the user object, then just set the user_id in your Studios model equal to the user. A better naming convention, however, would be to name the field "user" (in order to avoid confusion):

class Studios(models.Model):
        user = models.ForeignKey(User)

Make sure that you save studio_name as well because it is a required field in your Studios model.

like image 50
Aaron Lelevier Avatar answered Oct 03 '22 04:10

Aaron Lelevier