开发者

Loop through two querysets while highlighting differences in django

I have a model class defined as below:

class Vehicle(models.Model):
    stock_number = models.CharField(max_length=6)
    vin = models.CharField(max_length=17)
    year = models.PositiveSmallIntegerField()
    manufacturer = models.CharField(max_length=15)
    manufacturer_popularity = models.CharField(max_length=1, blank=True, null=True)
    model = models.CharField(max_length=20)
    series = models.CharField(max_length=35, blank=True, null=True)
    exterior_colour = models.CharField(max_length=25)
    interior_colour = models.CharField(max_length=25)
    interior_type = models.CharField(max_length=25)
    doors = models.PositiveSmallIntegerField()
    passengers = models.PositiveSmallIntegerField()
    body_style = models.CharField(max_length=30)
    transmission = models.CharField(max_length=50)
    fuel_type = models.CharField(max_length=50)
    engine_capacity = models.DecimalField(max_digits=2, decimal_places=1)
    cylinders = models.PositiveSmallIntegerField()
    drive_train = models.CharField(max_length=50)

What I want to do is to query two databases based on the above class then display the data in a template while showing the differences between each paired Vehicle entry in red. The querying bit isn't the main issue I guess since I'll need something like:

set1 = Vehicle.objects.all()
set2 = Vehicle.objects.using('other_db').all()

But how can I have the pairing and display being handled? I hope someone can show me the light here.

UPDATE 1

vehicles = []
for v1 in set1:
    for v2 in set2:
        if v1['stock_number'] == v2['stock_number']:
            vehicle1 = vehicle2 开发者_运维问答= {}
            vehicle1['vehicle_1'] = v1
            vehicle2['vehicle_2'] = v2
            vehicles.append(vehicle1)
            vehicles.append(vehicle2)
        else:
            vehicle1 = vehicle2 = {}
            vehicle1['vehicle_1'] = v1
            vehicle2['vehicle_2'] = {}
            vehicles.append(vehicle1)
            vehicles.append(vehicle2)


The idea

from itertools import izip

for obj1, obj2 in izip(set1, set2):
    for field in obj1._meta.fields:
        if field.value_from_object(obj1) == field.value_from_object(obj2):
            print 'Same value', field.value_from_object(obj1)
        else:
            print 'Obj1 has', field.value_from_object(obj1)
            print 'Obj2 has', field.value_from_object(obj2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜