我查詢的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對象的任何代碼。 –