Python 如何将django模型保存到数据库?save()方法不起作用

Python 如何将django模型保存到数据库?save()方法不起作用,python,django,django-models,django-views,django-forms,Python,Django,Django Models,Django Views,Django Forms,嗨,我在将django模型保存到数据库时遇到问题 我的应用程序使用Google signin进行身份验证,如果用户的配置文件在身份验证后不在数据库中,它会将用户重定向到一个表单,用户可以在该表单中创建配置文件 问题在于,每当用户创建配置文件时,它都不会保存到数据库中。 下面我附上了我的管理页面的图片。(您将看到没有纵断面对象) 我还附加了forms.py、models.py和views.py 视图.py def register(request): user_email = Profile.o

嗨,我在将django模型保存到数据库时遇到问题

我的应用程序使用Google signin进行身份验证,如果用户的配置文件在身份验证后不在数据库中,它会将用户重定向到一个表单,用户可以在该表单中创建配置文件

问题在于,每当用户创建配置文件时,它都不会保存到数据库中。

下面我附上了我的管理页面的图片。(您将看到没有纵断面对象) 我还附加了forms.py、models.py和views.py

视图.py

def register(request):

user_email = Profile.objects.filter(email = request.user.email).exists()
if request.user.is_authenticated:
    if user_email:
        return redirect('blog-home')
    else:
        if request.method == 'POST':
            profile = Profile()
            profile_form = CreateProfileForm(request.POST, instance=profile)
            if profile_form.is_valid():
                profile.user = request.user
                profile.email = request.user.email
                profile_form.save()
                messages.success(request, f'Your account has been created! You are now able to log in')
                return redirect('blog-home')
        else:
            profile_form = CreateProfileForm()

    context = { 'form': profile_form  }

return render(request, 'users/register.html', context)
class Profile(models.Model):
    user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
    username = models.CharField(max_length = 15, default = '')
    first_name = models.CharField(max_length=20, default='')
    last_name = models.CharField(max_length=20, default='')
    email = models.CharField(max_length = 25, default = '')
    #image = models.ImageField(null=True, blank=True, upload_to='media/user/profile')
    #interests

    def __str__(self):
        return f'{self.first_name} Profile'
class CreateProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'username']
型号.py

def register(request):

user_email = Profile.objects.filter(email = request.user.email).exists()
if request.user.is_authenticated:
    if user_email:
        return redirect('blog-home')
    else:
        if request.method == 'POST':
            profile = Profile()
            profile_form = CreateProfileForm(request.POST, instance=profile)
            if profile_form.is_valid():
                profile.user = request.user
                profile.email = request.user.email
                profile_form.save()
                messages.success(request, f'Your account has been created! You are now able to log in')
                return redirect('blog-home')
        else:
            profile_form = CreateProfileForm()

    context = { 'form': profile_form  }

return render(request, 'users/register.html', context)
class Profile(models.Model):
    user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
    username = models.CharField(max_length = 15, default = '')
    first_name = models.CharField(max_length=20, default='')
    last_name = models.CharField(max_length=20, default='')
    email = models.CharField(max_length = 25, default = '')
    #image = models.ImageField(null=True, blank=True, upload_to='media/user/profile')
    #interests

    def __str__(self):
        return f'{self.first_name} Profile'
class CreateProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'username']
forms.py

def register(request):

user_email = Profile.objects.filter(email = request.user.email).exists()
if request.user.is_authenticated:
    if user_email:
        return redirect('blog-home')
    else:
        if request.method == 'POST':
            profile = Profile()
            profile_form = CreateProfileForm(request.POST, instance=profile)
            if profile_form.is_valid():
                profile.user = request.user
                profile.email = request.user.email
                profile_form.save()
                messages.success(request, f'Your account has been created! You are now able to log in')
                return redirect('blog-home')
        else:
            profile_form = CreateProfileForm()

    context = { 'form': profile_form  }

return render(request, 'users/register.html', context)
class Profile(models.Model):
    user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
    username = models.CharField(max_length = 15, default = '')
    first_name = models.CharField(max_length=20, default='')
    last_name = models.CharField(max_length=20, default='')
    email = models.CharField(max_length = 25, default = '')
    #image = models.ImageField(null=True, blank=True, upload_to='media/user/profile')
    #interests

    def __str__(self):
        return f'{self.first_name} Profile'
class CreateProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'username']

看起来您与配置文件和配置文件表单混淆了,因此覆盖了您的更改。您应该调用profile.save()而不是profile\u form.save(),因为您已经对profile进行了更改(添加了用户和电子邮件)。无需在is_valid()方法中实例化新的Profile()。试着这样做:

if request.method == 'POST':
    profile_form = CreateProfileForm(request.POST)
    if profile_form.is_valid():
        profile = profile_form.save(commit=False)
        profile.user = request.user
        profile.email = request.user.email
        profile.save()
        messages.success(request, f'Your account has been created! You are now able to log in')
        return redirect('blog-home')

你说的有道理。然而,当我尝试它时,我最终还是回到了原来的位置。奇怪的是,当我转到Python shell时,它显示了正在创建的所有对象。它只是因为某些原因没有显示在管理数据库中哦,我想我知道问题出在哪里了!我从来没有将我的模型导入到我的admin.py文件lolYup,它现在可以工作了。我忘了将我的配置文件模型注册到admin.py文件中。谢谢你的帮助