我正在使用Django 2.0
在应用程序的 models.py 文件中,我有一个模型 Abc ,模型管理器为 AbcManager ,queryset为 AbcQueryset < / strong>
class AbcQueryset(models.query.QuerySet):
def search(self, query):
q = Q(show_to_all=True)
if query:
q.add(Q(walmart_record__name__icontains=query) | Q(amazon_record__category__icontains=query), q.connector)
if q:
return self.filter(q).distinct()
else:
return self.all()
class AbcManager(models.Manager):
def get_queryset(self):
return AbcQueryset(self.model, self._db)
def update_or_create(self, walmart_record, amazon_record, **save_data):
record = Abc.objects.filter(
walmart_record_id=walmart_record.pk,
amazon_record_id=amazon_record.pk
)
print(record.query)
print(record)
print('walmart_record: ' + str(walmart_record.pk))
print('amazon_reocrd: ' + str(amazon_record.pk))
if record.exists():
print('~~~~~~~~~~~~~~~~~~ record alreay exists')
record.update(**save_data)
print(record.first().pk)
return record.first(), True
record, created = self.get_queryset().get_or_create(
walmart_record=walmart_record,
amazon_record=amazon_record,
**save_data
)
print('~~~~~~~~~~~~~~~~~~ record created')
print(record.pk)
return record, created
def search(self, query):
return_data = self.get_queryset().search(query).order_by('-created')
class Abc(models.Model):
walmart_record = models.ForeignKey(WalmartRecord, on_delete=models.CASCADE)
amazon_record = models.ForeignKey(AmazonRecord, on_delete=models.CASCADE, null=True)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
objects = AbcManager()
def __str__(self):
return self.arbitrase
def get_absolute_url(self):
return reverse('detail', kwargs={'pk': self.pk})
但是在调用** update_or_create()时,它总是在创建新记录。**
从 record.query 生成的正在运行的sql脚本在phpmyadmin中运行正常,结果即将到来,但是print(record)
给出的空白列表为
<ArbitraseRecordQueryset []>