1

What is the best way to extend third party model in django 1.5?
Suppose I have 3rd party model called Feedback:

class Feedback(models.Model):
    user = models.ForeignKey(
        'auth.User',
        verbose_name=_('User'),
        related_name='feedback_form_submissions',
        blank=True, null=True,
    )

    email = models.EmailField(
        verbose_name=_('Email'),
        blank=True,
    )
    ...

I have my own user class. I need to override the user field. email I need to make blank=False and presumably want to add another Field to the model. What is the best way to do this?

Unicorn
  • 1,397
  • 1
  • 15
  • 24
  • 2
    unfortunately they should have used the correct conventions in creating an app that supports custom user model (using AUTH_USER_MODEL) it might be easiest and cleanest to fork their project and make the correct updates – dm03514 May 27 '14 at 20:33

2 Answers2

2

Depending on the circumstance, that is, which 3rd party app you are using, you can approach this in a variety of ways:

  1. Explicity inheriting from a model:

    class CustomFeedback(Feedback): #etc

  2. Create a custom app: Extend the app by doing a django-admin.py startapp AppName_custom which is a technique I have used with some apps. In that case you will want to inheret from a class like above but intercept DB methods like save or clean.

  3. Fork the app. This is easily the most difficult way of doing this if you are inexperienced with Django. Just clone or fork via git or mercurial then add whatever code you need to modify the behavior of the app.

As a rule of thumb (pardon the expression) you shoudln't modify the User model since the Auth module is nicely comparmentalized. Instead you should use an app like django-profiles to add data to models or extend Forms.

theWanderer4865
  • 861
  • 13
  • 20
  • I beg you pardon, but if I extend the the app by inheriting from the class how can I change the user field? Django explicitly forbids the field hiding and overriding https://docs.djangoproject.com/en/1.5/topics/db/models/#field-name-hiding-is-not-permitted – Unicorn May 27 '14 at 21:54
  • Sorry, I'm not sure I understand. If you are customizing a form you can just leave it out of the ```fields``` attribute. If you are trying to modify the model fields themselves I would think about how that impacts the principle "Don't Repeat Yourself." Again, I'm not sure I understand your follow-up. – theWanderer4865 May 27 '14 at 22:55
1

Have you looked at this post? How to make email field unique in model User from contrib.auth in Django

I think it answers everything you want.

Django also includes documentation on how to do this here: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-user

You can also designate a custom User model for Django to use with AUTH_USER_MODEL = 'myapp.MyUser'. From there you can set your custom fields.

Community
  • 1
  • 1
Ian Leaman
  • 135
  • 7