How to do this kind of relationship in Django?

Asked

Viewed 144 times

1

I have the models "Subscriber":

class Assinante(models.Model):
    name = models.CharField("Nome", max_length=32, null=False, blank=True)
    phone = models.CharField("Telefone", max_length=11, null=True, blank=True)
    email = models.EmailField("E-mail", null=True, blank=True)
    plans = models.ForeignKey("Plano", null=True, blank=True)
    start_date = models.DateField("Data da assinatura", null=False, blank=False)
    due_date = models.DateField("Vencimento", null=False, blank=False)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Assinantes"

And the models "Plan":

class Plano(models.Model):
    name = models.CharField("Nome", max_length=32, null=False, blank=False, unique=True)
    price = models.FloatField("Valor pago", default=00.00)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Planos"

As it is possible to notice in "Subscriber" I have a foreign key referencing the "Plan" of the "Subscriber". What I want is to have a "Subscriber" field that stores the value of the variable price set in "Plan", but this value needs to be related to the table of the plan chosen in the variable plans, for example I registered a name plan Gold with the price 79.99, when I add a subscriber and set his plan as Gold I want to automatically value the column Amount paid is 79.99. Example below.

I need this to work on a Django-Admin table, can someone help me ?

Assinantes
| Nome   | Telefone     | E-mail            | Planos    | Valor pago |
| Carlos | 88 952684179 | [email protected] | Gold      | 79,99      |
| Mario  | 88 910101799 | [email protected] | Platinium | 119,99     |
Planos
| Plano     | Preço  |
| Prata     | 29,99  |
| Gold      | 79,99  |
| Platinium | 119,99 |

1 answer

1


From what I understand you only need to show the data in the Django admin table this way. To do so, create a class in the admin file, with the following:

class AssinanteAdmin(admin.ModelAdmin):
    resource_class = Assinante
    list_display = ('name', 'phone', 'email', 'get_name', 'get_price')
    search_fields = ['name', 'phone']

    def get_name(self, obj):
        return obj.plans.name

    def get_price(self, obj):
        return obj.plans.price

admin.site.register(Assinante, AssinanteAdmin)

That is, create table settings and for your FK you create a function for each attribute you want to show on the grid.

Upshot.

inserir a descrição da imagem aqui

  • That’s exactly what I wanted, thank you very much helped me!

  • In this example, if I change the value of the plan all relationships are updated correct? but if I want new registrations to use the new value but active registrations he respects the original value has how? something like entering the value of the table and plans in the subscriber table?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.