5

I have the follow Models (an example, not really one):

class ModelB(models.Model):
  name = models.CharField(max_length=50)

  def __str__(self):
    return self.name

class ModelA(models.Model):
  code = models.CharField(max_length=50, unique=True, help_text="Code unique")
  foreignkey = models.ForeignKey(ModelB, unique=True)

And in my admin.py I have:

class ModelBAdmin(admin.ModelAdmin):
  list_display = ('name',)

class ModelAAdmin(admin.ModelAdmin):
  list_display = ('code', 'foreignkey')

admin.site.register(ModelA, ModelAAdmin)
admin.site.register(ModelB, ModelBAdmin)

I would make something similar to:

class ModelBAdmin(admin.ModelAdmin):
   list_display = ('name', 'code')

The code must be the code-relation from ModelA-code. How I can make this?

P.D.: Sorry for my english...

Thx a lot, Antonio.

antonio
  • 540
  • 1
  • 5
  • 19
  • 1
    ModelB doesn't have a single related code from ModelA, since it's a one-to-many relationship - there will be many related codes. What do you want to display? – Daniel Roseman Jul 31 '12 at 16:06
  • I want show the code from ModelA, because every ModelA must have a ModelB, but if I want edit ModelB from the Adminsite is not easy find It. – antonio Jul 31 '12 at 16:15
  • Yes, every ModelA must have a ModelB, but that means that each ModelB has *many* ModelAs. Again, what do you want to display? – Daniel Roseman Jul 31 '12 at 16:19
  • @antonio: so what do you show when one ModelB instance is referenced by 10 ModelA instances? – vartec Jul 31 '12 at 16:19
  • @DanielRoseman each ModelB has only one ModelA, because in the foreignkey I wrote "unique=True". The problem is only that if I want edit a ModelB, it's difficult findt It. – antonio Jul 31 '12 at 16:21
  • @vartec Hi vartec, each ModelB has only one ModelA, because in the foreignkey I wrote "unique=True". The problem is that if I want edit a ModelB, it's difficult findt It by the AdminSite. I would that in the adminsite of "ModelB" I could seach it by the code from ModelA, and not by his "name". – antonio Jul 31 '12 at 16:24
  • just replace it with OneToOneField: https://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield – vartec Jul 31 '12 at 16:34

3 Answers3

9

Your comments show that actually what you're interested in isn't the list display at all, but the editing. For that you should use inline forms:

class ModelAInline(admin.StackedInline):
  model = ModelA

class ModelBAdmin(admin.ModelAdmin):
  list_display = ('name',)
  inlines = [ModelAInline]

admin.site.register(ModelA, ModelAAdmin)

Now, on the edit form, each ModelA has a list of ModelBs underneath which you can edit directly there.

(Note that instead of using a ForeignKey with unique=True, you should probably use a OneToOneField.)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
4

You can define a custom item in list_display this way:

class ModelBAdmin(admin.ModelAdmin):

    def modelA_Codes(self, inst):
        return ','.join([b.code for b in inst.modela_set.all()])

    list_display = ('name', 'modelA_Codes')

Since one modelB can be attached to multiple modelA items, you probably need to return a list of the applicable codes for the specified ModelB.

Tisho
  • 8,320
  • 6
  • 44
  • 52
3

Thanks to @vartec , @DanielRoseman and @Tisho . Finally, with your suggesions I have made the next (I think no it's very efficient... But the other methods raise errors...)

class SubvencionCAAdmin(admin.ModelAdmin):
  search_fields = ['nombre', 'tipo', 'resumen']

  def Subvencion_Code(self):
      lista_subvenciones = Subvencion.objects.all()
      for subvencion in lista_subvenciones :
            if (self.nombre == subvencion.CA.nombre):
                return subvencion.codigo

  list_display = ('nombre', Subvencion_Code)

The SubvencionCAAdmin is the equivalent to "ModelB" and "Subvencion" the "ModelA".

Thanks a lot

antonio
  • 540
  • 1
  • 5
  • 19
  • 1
    You can also upvote the answers if they helped you(which seems to be the case), and to accept one. – Tisho Jul 31 '12 at 18:07
  • 1
    @Tisho I can't upvote the answer, I'm only have 10reputation... The minimun to upvote is 15. – antonio Jul 31 '12 at 22:00