我有标准的django User model
,我有一个扩展模型Member
。成员模型已连接到订阅模型。
class Type(models.Model):
limit = models.IntegerField()
name = models.CharField(max_length=50)
default = models.BooleanField()
def __str__(self):
return self.name
class Subscription(models.Model):
started = models.DateField()
type = models.OneToOneField(Type)
def __str__(self):
return self.type.name
class Member(models.Model):
user = models.ForeignKey(to=User)
number = models.CharField(max_length=10)
match_code = models.CharField(max_length=50)
parent = models.ForeignKey("self", null=True, blank=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=255)
postcode = models.CharField(max_length=10)
city = models.CharField(max_length=100)
country = models.ForeignKey(to=Country, null=True)
language = models.ForeignKey(to=Language)
telephone = models.CharField(max_length=50)
mobile = models.CharField(max_length=50)
main_email = models.EmailField(null=True, blank=True)
active = models.BooleanField()
subscription = models.ForeignKey(Subscription)
def __str__(self):
return self.name
当我创建新用户时,以及当我想将其保存到数据库时,我按如下方式执行:
language = Language.objects.get(key="EN")
country = Country.objects.get(key="BE")
# Add to user model
user = User()
user.username = request.POST['email']
user.first_name = request.POST['firstname']
user.last_name = request.POST['lastname']
user.email = request.POST['email']
user.set_password(request.POST['password'])
user.save()
# Add subscription
subscription = Subscription()
subscription.started = datetime.date.today()
subscription.type = Type.objects.filter(default=True)
last_subscription = Subscription.objects.all().order_by('-id')[0]
# Get the last field from user model
last_user = User.objects.all().order_by('-id')[0]
# Add to member model with the last user
member = Member()
member.number = request.POST['member_id']
member.address = request.POST['address']
member.postcode = request.POST['postcode']
member.city = request.POST['city']
member.country = country
member.telephone = request.POST['telephone']
member.mobile = request.POST['mobile']
member.user = last_user
member.language = language
member.active = False
member.subscription = last_subscription
member.save()
但我认为我多次访问数据库。有没有更好的解决方案呢?可以用一个查询完成吗?
答案 0 :(得分:3)
这里有些不对劲
subscription.type = Type.objects.filter(default=True)
您将整个查询集设置为subscription.type此代码会在此处产生错误。其次,不要将默认值用作模型字段名称。
last_subscription = Subscription.objects.all().order_by('-id')[0]
不太确定你在这里想做什么。高效且无错误的方法是保存订阅对象并使用它为下一次操作生成的id。
user.username = request.POST['email']
user.first_name = request.POST['firstname']
user.last_name = request.POST['lastname']
user.email = request.POST['email']
user.set_password(request.POST['password'])
将数据直接传递给模型并不是一个好主意。您应该首先使用django表单来验证它们。
last_user = User.objects.all().order_by('-id')[0]
# Add to member model with the last user
这可能是您刚刚保存的用户,也可能不是。很可能另一个实例在此线程之后就已经保存了另一个用户。使用user.id更安全(如已经提到的关于订阅)
答案 1 :(得分:1)
实际上,您不需要获取上次订阅或上次用户实例。您可以(而且应该!)只使用您的订阅和用户对象。因为在调用save方法后它们将具有id。"Auto-incrementing primary keys"
此外,您似乎从未保存订阅实例。