我以前用的是ForeignKey.
模型
class Phone_request_list(models.Model):
who = models.IntegerField(null=True)
whose = models.ForeignKey(User)
视图
def phone_request(request):
user_id = request.user.id
uuid = request.GET.get('uuid')
profile_id = profiles.objects.get(uuid=uuid).user_id
p,created = Phone_request_list.objects.get_or_create(who=user_id,whose_id=profile_id)
p.save()
但现在我想将我的模型改为多个领域
class Phone_request_list(models.Model):
who = models.IntegerField(null=True)
whose = models.ManyToManyField(User)
这次我如何检查get_or_create中的谁.请帮我.
最佳答案
def phone_request(request):
user_id = request.user.id
uuid = request.GET.get('uuid')
profile_id = profiles.objects.get(uuid=uuid).user_id
p,created = Photo_request_list.objects.get_or_create(who=user_id)
p.whose.add(profiles.objects.get(uuid=uuid))
p.save()
顺便说一下,当你使用ForeignKey时你不需要最后的p.save(),但是你现在有了M2M