使用 Python 3.4 和 Django 2.0.2
models.py
class Userinfo(models.Model):
useruid = models.BigAutoField(db_column='UserUID', primary_key=True)
useremail = models.CharField(
db_column='UserEmail', unique=True, max_length=100)
userpassword = models.CharField(db_column='UserPassword', max_length=128)
passwordsalt = models.CharField(db_column='PasswordSalt', max_length=128)
userstatus = models.IntegerField(db_column='UserStatus', default=0)
username = models.CharField(
db_column='UserName', max_length=100, default=0)
class Meta:
managed = False
db_table = 'userinfo'
class Postinfo(models.model):
postuid = models.BigAutoField(db_column='PostUID', primary_key=True)
content = content = models.TextField(db_column='Content')
useruid = models.ForeignKey(
Userinfo, db_column='UserUID', on_delete=models.CASCADE)
class Meta:
managed = False
db_table = 'postinfo'
查询集
postquery = models.Postinfo.objects.all().select_related("useruid").order_by(
'-postuid')
postquery是什么?
<QuerySet [{'content': 'test','postuid': 1, 'useruid_id': 1},OtherPosts...>
并且
userinfoquery = postquery.values("useruid_id__username","useruid_id__userstatus","useruid_id")
userinfoquery is
<QuerySet [{'useruid_id__username': 'firstuser','useruid_id__userstatus': 0, 'useruid_id': 1},OtherUsers...>
我希望将 useruid_id__username 翻译为 username
将 useruid_id__userstatus 翻译为 userstatus
将 useruid_id 翻译为 useruid
我尝试过这样做
userinfoquery = postquery.values(
useruid="useruid_id", username="useruid_id__username", userstatus="useruid_id__userstatus")
这段代码会引发异常。
QuerySet.annotate() received non-expression(s): useruid_id, useruid_id__uesrname, useruid_id__userstatus.
如何删除用户uid_id__?