3

I've been struggling with a situation in Django/Mysql.

There is this column in a table that has a primary key and foreign key at the same time. This column has a one to many relation with an intermediary table.

It's a list of states linked to plant species. Some species can be found in more than one state.

  • Species (table 1) Columns: Species_id | specie_name
  • Species_to_states (table 2) Columns: Species_id | State_id
  • States (table 3) Columns: States_id, State_name

Models.py (code below)

class Listaflor(models.Model):
    especie = models.OneToOneField(Flora2Estado, models.DO_NOTHING, primary_key=True)
class Flora2Estado(models.Model):
    estado = models.OneToOneField(Estados, models.DO_NOTHING, primary_key=True)
    especie = models.ForeignKey('Listaflor', models.DO_NOTHING)
class Estados(models.Model):
    estado_id = models.AutoField(primary_key=True)
    estado_nome = models.CharField(max_length=100, blank=True, null=True)
    nome_abbr = models.CharField(max_length=2, blank=True, null=True)
    criadoem = models.DateTimeField(db_column='criadoEm')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'estados'
        verbose_name = "Estados"
        verbose_name_plural = "Estado"
    def str(self):
        return self.estado_nome+ " (" + self.nome_abbr+")"

The point is that only one object is being displayin django admin (check the image below). So i would like to show multiple fields within a row.

enter image description here

Can someone help me with that?

Thank you so much

class floAdmin(admin.ModelAdmin):

    list_display = ('especie','familia','regiaopred','emailautor','aprovado','datacriado','especie_0')
    fieldsets = (
        ('Editar:', {
            'fields': (

                'especie','familia','regiaopred','emailautor','aprovado','datacriado','especie_0'
                #'pes_vinculo_tipo'
                )
            }),
        )

Just like the image below, I want to display "Miami, Ny and Maine", for example, in only one row in django admin. Is that possible?

enter image description here

Jay
  • 109
  • 8
  • I'm confused... why do you have `Listaflor.especie` as a `OneToOne` field to `Flora2Estado` and also have `Flora2Estado.especie` as a `ForeignKey` to `Listaflor`? – ChrisGPT was on strike Nov 17 '20 at 21:45
  • Can you share your Admin model please? – tarasinf Nov 17 '20 at 21:47
  • @Chris So do I just have to set oneToMany on listaflor.especie? – Jay Nov 17 '20 at 21:50
  • Thank you both for your attention! – Jay Nov 17 '20 at 21:50
  • @Jay, I'm not sure what you're trying to accomplish. But yes, it's unusual to have both of these relationships set up. – ChrisGPT was on strike Nov 17 '20 at 21:51
  • @Chris , Ok, sorry. I did what you told me. Thanks a lot. – Jay Nov 17 '20 at 21:55
  • @tarasinf, I've edited my question and inserted the code you asked me. Thanks. – Jay Nov 17 '20 at 21:58
  • @Chris , I want to display "State_Name" (column: estado_name - table: estados) within (table listaflor) in Django Admin ... The point is that we could have more than one state name for a single specie (Column: especie | table: listaflor) . That's why we have that intermediary table flora2estado. So, I would like to display multiple "estados" on a row listaflor table in Django Admin .. Sorry about my english. I hope I was more clear this time. – Jay Nov 17 '20 at 22:07
  • @Chris , I've edit my initial question once again. Since my english is causing me trouble, I 've drawn a table with exemples. Thank you so much – Jay Nov 17 '20 at 22:43

2 Answers2

1

I don't understand what info you want to get in a cell, but want to suggest you to use the next approach:

class floAdmin(admin.ModelAdmin):

    list_display = ('especie', 'familia', 'regiaopred', 'emailautor', 'aprovado', 'datacriado', 'especie_0', 'custom_field', )
    fieldsets = (
        ('Editar:', {
            'fields': ('especie', 'familia', 'regiaopred', 'emailautor', 'aprovado', 'datacriado', 'especie_0', 'custom_field', )
            }),
        )

    def custom_field(self, obj):
        data_list_to_show = []  # get data based on obj
        return data_list_to_show 
tarasinf
  • 796
  • 5
  • 17
  • Hey, thanks a lot for your suggestion. What exactly should i put into the data_list_to_show? I want to display multiple "estado_nome" in the same row. Once again, thank you so much for your time. – Jay Nov 17 '20 at 22:21
  • in `data_list_to_show` you should put those multiple "estado_nome" objects (id, or link to object, anything you need). – tarasinf Nov 17 '20 at 22:31
  • I've edit my initial question once again. Since my english is causing me trouble, I 've drawn a table with exemples. Thank you so much. – Jay Nov 17 '20 at 22:43
  • I've tried the code you suggested : def custom_field(self, obj): data_list_to_show = [Estados.estado_nome], The following message is being displayed in admin: – Jay Nov 17 '20 at 23:05
  • Your AdminModel class should be related to specific DjangoModel, what is that model in your case? You can't do like this `[Estados.estado_nome]`, here you try to get class property, where you need to use object property, please try to get all info you need from `obj`. – tarasinf Nov 18 '20 at 06:17
1

If I true understood) because I had the same problem. This helped me:

fieldsets=(        
       ("My Group",{"fields": (tuple(['field1','field1']),),}), 
    )

Wrap those fields on their own tuple. There was answer Django admin display multiple fields on the same line