我想知道用django模型形式创建模型实例的正确方法是什么时候它包含一个必需的FK?我认为它可能与 target = get_target(self)
if target != 0:
angle = math.degrees(math.atan2(self.x - target[0], self.y - target[1])) + 90
self.sprite.image = pygame.transform.rotate(self.sprite.image,angle)
self.shoot(target)
if self.moving != False:
if abs(self.x - self.moving[0]) < 3 and abs(self.y - self.moving[1]) < 3:
self.moving = False
else:
self.move(self.moving)
if target == 0:
angle = math.degrees(math.atan2(self.x - self.moving[0], self.y - self.moving[1])) + 90
pygame.transform.rotate(self.sprite.image,angle)
if self.hp <= 0:
objects.remove(self)
return
self.sprite.rect = self.sprite.image.get_rect()
self.sprite.rect.topleft = [self.x - 20, self.y - 20]
window.blit(self.sprite.image, self.sprite.rect)
类属性有关,但在视图中我尝试在保存之前覆盖它。
型号:
exclude
形式:
class Profile(models.Model):
client = models.OneToOneField('auth.User')
first_name = models.TextField(blank=True,)
...
查看:
class ProfileForm(floppyforms.ModelForm):
class Meta:
exclude = ('client',)
model = Profile
widgets = {
'first_name': floppyforms.TextInput(attrs={'placeholder': 'First Name'}),
...
错误:
def post(self, request):
form = ProfileForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.client = User.objects.create(username=request.POST['email'],)
form.save()
return redirect('/success')
return redirect('/error')
干杯
答案 0 :(得分:5)
您的views.py中有错误。它应该是:
def post(self, request):
form = ProfileForm(request.POST)
if form.is_valid():
new_profile = form.save(commit=False)
new_profile.client = User.objects.create(username=request.POST['email'],)
new_profile.save()
return redirect('/success')
return redirect('/error')
您不应将客户端分配给表单,而应分配给内存中的实例new_profile
,然后调用new_profile.save()
将new_profile
保存到数据库。