gpt4 book ai didi

python - 在有两个模型的表单中保存之前设置ForeignKey

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:09 25 4
gpt4 key购买 nike

我已经四处寻找,但找不到任何适合我的情况的东西。我的应用程序中有一个 UserUserProfile 模型表单。

我的 forms.py 看起来像这样:

from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from .models import UserProfile

class UserForm(ModelForm):

class Meta:
model = User
fields = ('username', 'email', 'password')

class UserProfileForm(ModelForm):

class Meta:
model = UserProfile
fields = ('display_name', 'avatar', 'birthday', 'usertype', 'daw', 'usergenre')

我的views.py看起来像这样:

def RegisterView(request):
if request.method == 'POST':
form = UserForm(request.POST, prefix='uf')
form2 = UserProfileForm(request.POST, prefix='upf')
if form.is_valid():
if form2.is_valid():
form.save()
form2.user = form.username
form2.save()
return HttpResponseRedirect('/')
elif request.method == 'GET':
form = UserForm(prefix='uf')
form2 = UserProfileForm(prefix='upf')

return render(request, 'profile/register.html', {'form': form, 'form2': form2})

现在看到我的问题是,当我保存 UserProfile 实例时,它没有与新保存的 User 实例相关的所需 ForeignKey(User) 设置,如何解决我要在保存之前设置它吗?显然,我必须在保存 User 实例后执行此操作,但是如何获取我们刚刚创建的 User 实例的“request.user”并设置 ForeignKey 在 UserProfile 中?

模型.py

from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.images import get_image_dimensions
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from Submission.storage import OverwriteStorage

# Create your models here.
class UserProfile(models.Model):

def is_square_png(self):
if not self.name.endswith('.png'):
raise ValidationError("You may only upload .png files!")
else:
w, h = get_image_dimensions(self)
if not h == w:
raise ValidationError("This picture is not square! Your picture must be equally wide as its height.")
else:
if not (h + w) >= 1000:
raise ValidationError("This picture is too small! The minimum dimensions are 500 by 500 pixels.")
else:
if not (h + w) < 2000:
raise ValidationError("This picture is too big! The maximum dimensions are 1000 by 1000 pixels.")
return self

def generate_user_folder_avatar(instance, filename):
return "static/users/%s/%s.png" % (instance.user, 'avatar')

user = models.OneToOneField(User)
display_name = models.CharField(max_length=50, default="null")
avatar = models.ImageField(upload_to=generate_user_folder_avatar,storage=OverwriteStorage(),validators=[is_square_png],blank=True)

usertype_choices = [
('PR', 'Producer'),
('ME', 'Mastering Engineer'),
('CP', 'Composer'),
('SI', 'Singer'),
('AR', 'Artist'),
('DJ', 'Disk Jockey'),
('LI', 'Listener'),
('OT', 'Other'),
]

usertype = models.CharField(max_length=2,
choices=usertype_choices,
default='PR')
daw_choices = [
('FL', 'FL Studio'),
('AB', 'Live'),
('BT', 'Bitwig Studio'),
('CS', 'SONAR X3'),
('CB', 'Cubase'),
('AP', 'Apple Logic'),
('RE', 'Reason'),
('SO', 'Sony ACID'),
('PR', 'Pro Tools'),
('ON', 'Studio One'),
('MT', 'Digital Performer'),
('SA', 'Samplitude'),
('MC', 'Mixcraft'),
('RP', 'Reaper'),
('AR', 'Ardour'),
('OT', 'Other'),
('NO', 'None'),
]

daw = models.CharField(max_length=2,choices=daw_choices,default='NO')
usergenre = models.CharField(max_length=20,blank=True)
birthday = models.DateField(blank=True)

joined = models.TimeField(auto_now=True,auto_now_add=False)
followers = models.ManyToManyField(User, related_name="followers",blank=True)
status = models.TextField(max_length=300,blank=True)
pro = models.BooleanField(default=False)

最佳答案

在您的模型中将其作为外键用户

from yourapp.forms import UserForm, UserProfileForm

def register(request):
# Like before, get the request's context.
context = RequestContext(request)

# A boolean value for telling the template whether the registration was successful.
# Set to False initially. Code changes value to True when registration succeeds -- which is optional.
registered = False

# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
# Attempt to grab information from the raw form information.
# Note that we make use of both UserForm and UserProfileForm.
form = UserForm(data=request.POST, prefix='upf')
form2 = UserProfileForm(data=request.POST, prefix='upf')

# If the two forms are valid...
if form.is_valid() and form2.is_valid():
# Save the user's form data to the database.
user_reg = form.save()

# Now we hash the password with the set_password method.
# Once hashed, we can update the user object.
user_reg.set_password(user_reg.password)
user_reg.save()

# Now sort out the UserProfile instance.
# Since we need to set the user attribute ourselves, we set commit=False.
# This delays saving the model until we're ready to avoid integrity problems.
profile = form2.save(commit=False)
profile.user = user_reg

# Did the user provide a profile picture?
# If so, we need to get it from the input form and put it in the UserProfile model.
if 'avatar' in request.FILES:
profile.avatar = request.FILES['avatar']

# Now we save the UserProfile model instance.
profile.save()

# Update our variable to tell the template registration was successful --- which is optional.
registered = True

# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also be shown to the user.
else:
print form.errors, form2.errors

# Not a HTTP POST, so we render our form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
form = UserForm(prefix='upf')
form2 = UserProfileForm(prefix='upf')

# Render the template depending on the context.
return render_to_response(
'profile/register.html',
{'form': form, 'form2': form2, 'registered': registered},
context)

关于python - 在有两个模型的表单中保存之前设置ForeignKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35328589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com