我查询的Django内的PostgreSQL数据库,问题是:Django的查询数据库返回的查询集与交换价值
如果我有值的列 - ID = 1,用户名=胡说,DESC =不便在数据库中,查询返回正确的对象,但其值查询集 - ID =不便,用户名= 1,递减=胡说
芹苴我可以很容易地追加到一些合适的值,这是一个问题,当我尝试删除对象,因为在id上它需要一个int而不是charfield。如果有人能向我解释发生了什么以及如何解决问题,我会很高兴。
这里是我的代码:
waiting_users = OnlineUsers.objects.all()
print(waiting_users)
# If there are waiting users connect to one of them
if waiting_users.exists():
print("There are people on the list")
user_topair = waiting_users.first()
pair = PairUsers()
print(user_topair.username)
print(user_topair.reply_channel_name)
print(user_topair.id)
pair.username_a = username
pair.username_b = user_topair.reply_channel_name
pair.reply_channel_a = reply_channel_name
pair.reply_channel_b = user_topair.id
pair.save()
# user_topair.delete()
else:
# else put the user on the waiting list
print("There is no one in the list")
OnlineUsers(username, reply_channel_name).save()
message.reply_channel.send({'text': "Please wait while we find other players"})
我的模型:
class OnlineUsers(models.Model):
username = models.CharField(max_length=150, blank=False, null=False)
reply_channel_name = models.CharField(max_length=255, blank=False, null=False)
def __init__(self, username, reply_channel, *args, **kwargs):
super().__init__(*args, **kwargs)
self.username = username
self.reply_channel_name = reply_channel
class PairUsers(models.Model):
username_a = models.CharField(max_length=150, blank=False, null=False)
username_b = models.CharField(max_length=150, blank=False, null=False)
reply_channel_a = models.CharField(max_length=255, blank=False, null=False)
reply_channel_b = models.CharField(max_length=255, blank=False, null=False)
score_a = models.FloatField(blank=True, null=True)
score_b = models.FloatField(blank=True, null=True)
在此代码你在哪里看到的东西被交换? –
@DanielRoseman在3条打印线处我看到它们被交换,并且不像在分贝 – nitheism
请显示您的模型以及创建OnlineUsers对象的任何代码。 –