file = open('debug.txt', 'w')
db_objects = model.objects.all()
for i in db_objects:
db_filtered = model.objects.filter(input_address_db=i.input_address_db)
f.write('filtered_results = %s' % db_filtered)
...perform operations...
这是第一个循环后文件的输出:
db_filtered = [<model: model object>, <model: model object>, <model: model object>, <model: model object>]
我提前检查过,知道应该只有三个但是你可以看到调试文本显示四个。
上述两个对象是重复的一个。我知道这一点,因为当我尝试将四个对象转移到一个新数据库时,我收到了完整性错误。
created_on日期是主键,所以看到相同的结果两次也是令人惊讶的,因为我认为会抛出错误,我现在意识到也许你可能想要在查询集中重复相同的对象,这是为什么没有错误,但是我在解决实际发生的事情时遇到了一些麻烦。
在此先感谢您的帮助,我们非常感谢:)
答案 0 :(得分:4)
试试这个:
db_filtered = model.objects.filter(input_address_db=i.input_address_db).distinct()
无论如何,通过迭代db_objects
,您可以在多次迭代中迭代相同的input_address_db
。我会做类似的事情:
input_addess_dbs = set(d.input_address_db for d in model.objects.all())
for address in input_address_dbs:
db_filtered = model.objects.filter(input_address_db=address).distinct()
...
答案 1 :(得分:0)
我非常感谢大家的帮助,你的回答对我有很大的帮助,我只想澄清问题的最终结果 -
class bitcoin_deposit(models.Model):
input_address_db = models.CharField(max_length=34, null=False, blank=False)
deposit_amount_db = models.DecimalField(max_digits=16, decimal_places=8, validators=[MaxValueValidator(21000000)])
return_address_db = models.CharField(max_length=34, null=False, blank=False)
confirmations_db = models.IntegerField(validators=[MinValueValidator(-1)])
created_on_db = models.DateTimeField(primary_key=True, auto_now_add=True)
updated_on_db = models.DateTimeField(auto_now=True)
txlist_db = models.CharField(max_length=64, null=False, blank=False)
我通过将txlist_db
更改为主键来解决问题。
每当比特币网络告诉你的服务器你刚刚收到一些BTC时你就会非常快速地连续发送大约六次信号,事实上很快,即使我有这个模型到位 -
class bitcoin_deposit_txlist(models.Model):
txlist_db = models.CharField(max_length=64, null=False, blank=False)
这是脚本中的第一行代码,txlist
变量表示网络事务ID -
txlist = ''btc network transaction id''
if bitcoin_deposit_txlist.objects.filter(txlist_db=txlist).exists():
exit()
else:
bitcoin_deposit_txlist.objects.create(txlist_db=txlist)
我仍然设法不止一次地记录相同的记录。
以下是帮助我解决问题的一些存款对象的价值 -
[
{'return_address_db': u'mpfUAfK8bHi4VejvV4WhjEar3yQvPoX1Yn', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 3, 478489, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 12, 30, 804646, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.14384551'), 'confirmations_db': 1, 'txlist_db': u'7fd82945b5b96ae4ff866b6689867038289b81ee947c8796007d3a9f1fa14b59', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'mmgC3dk3JRpCrS9UDS8QRHFokFd6N8bLz4', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 3, 491573, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 20, 44, 784596, tzinfo=<UTC>), 'deposit_amount_db': Decimal('1.13722512'), 'confirmations_db': 4, 'txlist_db': u'3f9ad1a2464ca33513f24f0e59274845b4ea04906ab0df054ef6668160f58cb2', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'motQ8oo7RcgMmXZVCHgmbq5jtkGnxJwa59', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 28, 550850, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 20, 31, 254955, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.42293583'), 'confirmations_db': 3, 'txlist_db': u'b8e7e819c0ab1dbae9aa106c3662e17eb3621749a4f72e8e8f1b991212f05971', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'motQ8oo7RcgMmXZVCHgmbq5jtkGnxJwa59', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 28, 559947, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 20, 31, 165585, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.42293583'), 'confirmations_db': 3, 'txlist_db': u'b8e7e819c0ab1dbae9aa106c3662e17eb3621749a4f72e8e8f1b991212f05971', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'mzgA1rXEuV59Zho34o5Lvko6LFpV9hWUq8', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 54, 666174, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 20, 44, 837193, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.11976356'), 'confirmations_db': 4, 'txlist_db': u'adbcfc5b70621bde28ba4f073eca7a12e70533fb57e617ddbce013b4f6bc063b', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'mzgA1rXEuV59Zho34o5Lvko6LFpV9hWUq8', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 54, 678403, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 20, 31, 293935, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.11976356'), 'confirmations_db': 3, 'txlist_db': u'adbcfc5b70621bde28ba4f073eca7a12e70533fb57e617ddbce013b4f6bc063b', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'mzgA1rXEuV59Zho34o5Lvko6LFpV9hWUq8', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 54, 688175, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 15, 53, 125196, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.11976356'), 'confirmations_db': 2, 'txlist_db': u'adbcfc5b70621bde28ba4f073eca7a12e70533fb57e617ddbce013b4f6bc063b', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'},
{'return_address_db': u'mzgA1rXEuV59Zho34o5Lvko6LFpV9hWUq8', 'created_on_db': datetime.datetime(2014, 8, 7, 8, 8, 54, 695733, tzinfo=<UTC>), 'updated_on_db': datetime.datetime(2014, 8, 7, 8, 20, 44, 611634, tzinfo=<UTC>), 'deposit_amount_db': Decimal('0.11976356'), 'confirmations_db': 4, 'txlist_db': u'adbcfc5b70621bde28ba4f073eca7a12e70533fb57e617ddbce013b4f6bc063b', 'input_address_db': u'mrcLfESBQqqRG3ZqKhwcjfVgsM4REvt2cL'}
]
请注意created_on_db
字段值的接近程度。
再次感谢:)