pythondjangodjango-modelssatchmo

Insert a new field in a specific admin view?


I'm trying to extend default Django's model with a new field. In localsite/models.py I have the following code:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from satchmo_store.contact.models import Organization

class OrganizationExtra(models.Model):
    organization = models.OneToOneField(Organization,
            verbose_name=_('Organization'), primary_key=True )
    vat_number   = models.CharField(_('VAT'), max_length=12)

Followed with run of ./manage.py syncdb which did created a new table for above model. So far so good.

Now I'm trying to add this new field in related Organization view in the admin interface. The following code registers the new menu, however the new vat_number field is not displayed in view of the related Organization model.

from django.contrib import admin
from localsite.models import ProductResource, OrganizationExtra

admin.site.register(OrganizationExtra)

The original Organization model is registered with

from satchmo_store.contact.models import Organization
from django.contrib import admin

class OrganizationOptions(admin.ModelAdmin):
    list_filter = ['type', 'role']
    list_display = ['name', 'type', 'role']

admin.site.register(Organization, OrganizationOptions)

Any idea how to insert my new field without touching original Satchmo sources ?


Solution

  • See the docs as usual.

    One possible way is to create new MyOrganization derived from Organization and register it in place of satchmo one

    Your models.py

    from django.db import models
    from django.utils.translation import ugettext_lazy as _
    from satchmo_store.contact.models import Organization
    
    class MyOrganization(Organization):
        vat_number   = models.CharField(_('VAT'), max_length=12)
    

    Your admin.py

    from django.contrib import admin
    from localsite.models import MyOrganization
    from satchmo_store.contact.models import Organization
    from satchmo_store.contact.admin import OrganizationOptions
    
    admin.site.unregister(Organization)
    admin.site.register(MyOrganization, OrganizationOptions)
    

    Another possible solution (if you wish to stick with OrganizationExtra) is to create custom form for Organization for admin interface and again reregister model. By it seems to me as more boilerplate and result will be the same.

    NB: in both cases DB structure would be the same, i.e. extra table would be created.