M
M
maxclax2014-12-17 18:14:06
Django
maxclax, 2014-12-17 18:14:06

Two ForeignKeys in the model, how to implement?

I have a currency model:

class Currencies(models.Model):
    name = models.CharField(verbose_name='Название', max_length=3, unique=True)

It will contain the following values: EUR, USD, UAH and the like.
Next, we have a model for exchange rates by directions:
class Rates(models.Model):
    class Meta:
        # делает уникальным направление обмена
        unique_together = ("_from", "_to")

    # эталон
    _from = models.ForeignKey(Currencies, verbose_name='Эталон')

    # валюта
    _to = models.ForeignKey(Currencies, verbose_name='Валюта')

    rate = models.FloatField(verbose_name='Курс')

Actually, in the admin panel, I expect to receive a choice of the exchange direction (for example, USD to UAH) and indicate the rate in this direction.
But the interpreter scolds me:
ERRORS:
currencies.Rates._from: (fields.E304) Reverse accessor for 'Rates._from' clashes with reverse accessor for 'Rates._to'.
  HINT: Add or change a related_name argument to the definition for 'Rates._from' or 'Rates._to'.
currencies.Rates._to: (fields.E304) Reverse accessor for 'Rates._to' clashes with reverse accessor for 'Rates._from'.
  HINT: Add or change a related_name argument to the definition for 'Rates._to' or 'Rates._from'.

In swearing, we are talking about feedback, but I don’t really need it. How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2014-12-17
@maxclax

class Rates(models.Model):
    class Meta:
        # делает уникальным направление обмена
        unique_together = ("_from", "_to")

    # эталон
    _from = models.ForeignKey(Currencies, verbose_name='Эталон', related_name="rates_from")

    # валюта
    _to = models.ForeignKey(Currencies, verbose_name='Валюта', related_name="rates_to")

    rate = models.FloatField(verbose_name='Курс')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question