[英]How to access value in class' atribute in models.py Django
我想在views.py 中使用我的function 更改用户名和问题。 这是我的models.py:
class Choice(models.Model):
username = models.ForeignKey(Users, null=True, on_delete=models.CASCADE)
question = models.ForeignKey(Questions, null=True, on_delete=models.CASCADE)
answer = models.CharField(null=True,max_length=50)
我还想使用以下行更改用户名内部选择:
if username == str(Users.objects.latest('name')):
Choice.objects.username = Users.objects.latest('name')
是否有类似Choice.objects.something
可以检索“用户名”中的值并允许我更改它的东西?
您需要使用变量来存储 Choice 实例,设置用户名,然后调用Model.save()
将其写入数据库。
choice = Choice.objects.get(whatever criteria you use to get the instance)
choice.username = Users.objects.latest("name").username
choice.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.