K
K
Kwad2020-04-16 12:41:42
Django
Kwad, 2020-04-16 12:41:42

How to output data from QuerySet to variables?

I have an order model, it contains order data.
They need to be retrieved during the admin action.
For example, extract address_city , later output to the console ( print(address_city) )

admin.py

def send_bot(modeladmin, request, queryset):
    print(queryset) # выводит имя пользователя так как def __str__ на имя заточен
    infor = Order.objects.filter(user=queryset) # выдает ошибку
    print(infor)
    print(address_city) # нужно вывести город заказа
send_bot.short_description = "Send order to manager"

class OrderAdmin(admin.ModelAdmin):
    list_display = ['user',
                    'ordered',
                    'ordered_date',
                    'being_delivered',
                    'received',
                    'novaposhta',
                    'coupon',
                    'address_city',
                    'address_point',
                    'address_name',
                    'address_surname',
                    'address_thirdname',
                    'address_phone',
                    'address_email',
                    'address_comment'
                    ]
    list_display_links = [
        'user',
        'novaposhta',
        'coupon'
    ]
    list_filter = ['ordered',
                   'being_delivered',
                   'received'
    ]
    actions = [send_bot]

admin.site.register(Category)
admin.site.register(Article)
admin.site.register(Item)
admin.site.register(OrderItem)
admin.site.register(Order, OrderAdmin)



models.py

class Order(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE)
    novaposhta = models.CharField('ТНН',max_length=20, blank=True, null=True)
    items = models.ManyToManyField(OrderItem)

    start_date = models.DateTimeField(auto_now_add=True)
    ordered_date = models.DateTimeField('Дата заказа')

    address_city = models.CharField('Город',max_length=40, blank=True, null=True)
    address_point = models.CharField('Отделение',max_length=40, blank=True, null=True)
    address_name = models.CharField('Имя',max_length=40, blank=True, null=True)
    address_surname = models.CharField('Фамилия',max_length=40, blank=True, null=True)
    address_thirdname = models.CharField('Отчество',max_length=40, blank=True, null=True)
    address_phone = models.CharField('Номер телефона',max_length=40, blank=True, null=True)
    address_email = models.CharField('E-Mail',max_length=40, blank=True, null=True)
    address_comment = models.CharField('Коментарий к заказу',max_length=250, blank=True, null=True)

    coupon = models.ForeignKey(
        'Coupon', on_delete=models.SET_NULL, blank=True, null=True)


    being_delivered = models.BooleanField('Выполнен',default=False)
    received = models.BooleanField('Подтвержден',default=False)
    ordered = models.BooleanField(default=False)

    def __str__(self):
        return self.user.username

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-04-16
@fox_12

infor = Order.objects.filter(user=queryset) # выдает ошибку

because it's a queryset
Return specific objects from queries. For example - I took the last. What you need in your case - you need to proceed from the logic of your application.
# вернули последнего пользователя из запроса, вернули последний Order 
infor = Order.objects.filter(user=queryset.last()).last()
print(infor.address_city) #вывели город заказа

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question