A
A
Alex Xmel2020-05-06 03:04:45
Django
Alex Xmel, 2020-05-06 03:04:45

How to get the field of the linked table?

There are a number of tables:

class UsedMoneyModel(models.Model):
    name = models.CharField(max_length=10, unique=True, verbose_name='Название валюты')

class MoneyTypeModel(models.Model):
    type = models.CharField(max_length=20, verbose_name='Тип платёжки')

class PaySystemModel(models.Model):
    type = models.ForeignKey(MoneyTypeModel, on_delete=models.CASCADE, verbose_name='Тип')  # Тип платёжки
    base = models.ForeignKey(UsedMoneyModel, on_delete=models.SET_NULL, verbose_name='База',
                             null=True)  # Базовая валюта

class ChangeModel(models.Model):
    pay_from = models.ForeignKey(PaySystemModel, on_delete=models.CASCADE, verbose_name='Клиент отдаёт',
                                 related_name='pay_from_change')


loop through the records from the ChangeModel table
for i in ChangeModel.objects.all():
    print(i.pay_from.base) - этот принт выводит None
    print(i.pay_from.type) - этот принт отлично всё выводит


I can’t understand why I can get to one table (MoneyTypeModel) without problems, but I can’t to another (UsedMoneyModel). They are almost identical

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noremorse_ru, 2020-05-06
@Desead

type is a reserved name in Python, change the field names and make sure the field actually stores something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question