Hey Bojan

There is a few ways you could do this (certainly more that what I'm
about to tell you). It depends on the effect you want to create.

Perhaps the simplest way is to inherit from the existing class. But
inheriting classes should only strictly be used as an `is a`
relationship. So perhaps you could justify it by thinking of your
Contact model as just a different, specialised version of the satchmo
Contact model. It's worth baring in mind the DB joins this type of
thing could add though, and the consequences these joins bring with
them. In a recent project I was working on, I imported the model I
needed, changed `abstract = True` and then inherited from it in one of
my models to do a similar sort of thing to what you're trying to
create:

from voting.models import Vote as VoteBase
VoteBase._meta.abstract = True

class SpecialVote(VoteBase):
    pass

There's a good TWiD article on inheriting models so you could take a
look at that to find out more about it
http://thisweekindjango.com/articles/2008/jun/17/abstract-base-classes-vs-model-tab/

The other option is to use a registering paradigm like that used in
Django-MPTT. You basically call a function with a bunch of models and
this function adds some properties to them. You can see it here in the
DJango-MPTT __init__.py file:
http://code.google.com/p/django-mptt/source/browse/trunk/mptt/__init__.py

If you read the docs for Django-MPTT you see that you have to add a
call to ``mptt.register`` with every model you want to have this
functionality. This function just adds a tonne of fields and
properties to your models using _meta. You can read more about that by
James Bennett:

http://www.b-list.org/weblog/2007/nov/04/working-models/

Have a poke around in loads of the project on Google Code though, see
what other people are doing, and how they're tackling problems like
you'll soon start having your own ideas about how to do stuff.

Hope all that helps.

RM

On Dec 4, 8:45 am, Bojan Mihelac <[EMAIL PROTECTED]> wrote:
> Hi all!
>
> I am looking for right way to add fields to model that is in another
> app. At first I created mysite application and in models.py put
>
> from satchmo.contact.models import Contact
> Contact.add_to_class("company_name", models.CharField(max_length =
> 200))
>
> that raises exception ProgrammingError sometimes:
> ProgrammingError: (1110, "Column 'company_name' specified twice")
>
> So now I check before if field is already in model:
>
> if 'company_name' not in Contact._meta.get_all_field_names():
>     Contact.add_to_class("company_name", models.CharField(max_length =
> 200))
>
> I have feeling that this is not right way to do. I'd appreciate any
> help, hint or thought on this.
> I also wonder how models are loaded in django.
>
> Bojan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to