gpt4 book ai didi

python - 使用 unique_together 和保存经过身份验证的用户时出现问题

转载 作者:行者123 更新时间:2023-12-01 08:04:38 26 4
gpt4 key购买 nike

我的代码中两个字段条目唯一性检查存在问题。

我使用 unique_together 定义了一个模型来检查每个用户字段记录的唯一性,但它接受该用户添加的重复条目。

模型.py

from django.db import models
from django.contrib.auth.models import User

class UserItem(models.Model):
definer = models.ForeignKey(User, on_delete=models.CASCADE)
item_name = models.CharField(max_length=50)
.
.
class Meta:
unique_together = ("definer", "item_name")

views.py

from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic.edit import CreateView, UpdateView, DeleteView

class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
#excluding "definer" field and inserting its value by form_valid
fields = ['item_name', . . .]

def form_valid(self, form):
form.instance.definer = self.request.user
return super().form_valid(form)

我期望发出警告并阻止用户添加自己之前添加的相同“item_name”的新记录,但它接受它们(没有警告)。

当我用其他字段替换“definer”时,它工作正常并警告重复记录。此外,当管理员添加记录时,它会起作用并且会出现预期的警告。

我猜想,这个问题是因为在“unique_together = ("definer", "item_name")"完成其作用之后,经过身份验证的用户被“def form_valid”插入为“definer”。另一方面,当“定义器”为空时,唯一性检查就会完成。

我应该怎么做才能解决这个问题?

编辑:添加完整模型

```` Full Model in model.py
class UserItem(models.Model):
item_type = models.CharField(max_length=12, verbose_name='Item type')
item_name = models.CharField(max_length=50)
bound = models.CharField(null=True, blank=True, default=None, max_length=4, verbose_name='Bound')
price = models.FloatField(default=0)
maximum_use = models.FloatField(default=0, verbose_name='Maximum use (%)’)
matterial = models.FloatField(null=True, blank=True, default=None, verbose_name='matterial (%)')
energy = models.FloatField(null=True, blank=True, default=None, verbose_name='energy (kcal/k)')

definer = models.ForeignKey(User, on_delete=models.CASCADE)

def __str__(self):
return "{}, name: {}, definer: {}".format(self.item_type, self.item_name, self.definer,)

def get_absolute_url(self):
return reverse('profile')

class Meta:
unique_together = ("definer", "item_name")
````


```` views.py after @Pedro suggestion to edit
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin

class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
fields = ['item_name', 'matterial', 'energy',]

def get_success_url(self):
return reverse('profile')

def form_valid(self, form):
user_item = form.save(commit=False)
user_item.definer = self.request.user
user_item.item_type = 'required'
user_item.bound = 'min'
try:
user_item.save()
except IntegrityError:
form.add_error('item_name', 'Item name is repeated')
return self.form_invalid(form)
return HttpResponseRedirect(self.get_success_url())
````

最佳答案

感谢@Pedro非常有用的提示;最后我可以通过修改他的代码来解决我的问题。

我还删除了 model.py 中的这部分:“类元: unique_together = ("定义者", "item_name")"

````views.py
class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
fields = ['item_name', 'matterial', 'energy',]

def form_valid(self, form):
user_items = form.save(commit=False)
item_name = user_items.item_name
qs = UserItemComposition.objects.filter(definer=self.request.user, item_name=item_name)
if qs.exists():
form.add_error('item_name', 'Item name is repeated')
return self.form_invalid(form)
form.instance.definer = self.request.user
return super().form_valid(form)
````

关于python - 使用 unique_together 和保存经过身份验证的用户时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55596953/

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