简体   繁体   English

Django:使用User作为外键保存表单

[英]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: 我想保存一个模型,使用Django User作为外键,我尝试创建一个像这样的新用户:

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: 然后把整个用户或用户ID,并不起作用,就像这样:

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. 没有任何反应,一切都保存在数据库,用户,组中,而不是用户作为外键,外键user_id每次都为null,请帮忙。

so, this is my code: 所以,这是我的代码:

Django Model: Django模型:

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: Django表格:

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: Django查看:

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? 任何的想法?

You are passing your data back to the studio_form here: 您将数据传递回studio_form

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. 实际上,您需要将数据传递给Studios模型,以便将其保存到数据库中。 The Form is only for cleaning data and temporary holding it until you access the model to save the data to the database. Form仅用于清理数据并临时保存,直到您访问模型以将数据保存到数据库。

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. 这将创建一个Studios对象,将数据保存到数据库。

user_id needs to be a User object (not the user.id ) since it is a foreign key to the User model . user_id需要是User对象(而不是user.id ),因为它是User model的外键。 So once you get the user object, then just set the user_id in your Studios model equal to the user . 因此,一旦获得用户对象,只需将Studios模型中的user_id设置为等于user A better naming convention, however, would be to name the field "user" (in order to avoid confusion): 但是,更好的命名约定是将字段命名为“user”(以避免混淆):

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 . 确保您也保存studio_name ,因为它是您的Studios model的必填字段。

我一直在围绕user.groups.add(group)这样做,也许这对你user.groups.add(group)吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM