[Django]-How to add extra fields to a django model by extending it?

2๐Ÿ‘

You can inherit the Plan models and add your own attributes:

from pinax-stripe.models import Plan

class MyPlan(Plan):
    # add your attributes
    pass

This works like normal inheritance in python, plus your custom attributes are migrated when you run a migration because the original pinax Plan is a subclass of models.Model.

However, be careful to not use attribute names that already exist in the pinax Plan model, since your new model will automatically take all the attibutes from Plan and Django cannot write migrations for duplicate fields.

๐Ÿ‘คMoses Koledoye

1๐Ÿ‘

You can simply subclass Plan and add whatever fields / methods you want:

from pinax-stripe.models import Plan

class UserProfile(Plan):
    #write the extra fields here
๐Ÿ‘คDevLounge

0๐Ÿ‘

Iโ€™d recommend you, use the OneToOne relationship like Django docs recommend to use in the User model

from pinax-stripe.models import Plan

class UserProfile(models.Model):
    plan = models.OneToOneField(Plan , on_delete=models.CASCADE)
    #write the extra fields here
๐Ÿ‘คarcegk

0๐Ÿ‘

You can download pinax folder from https://github.com/pinax/pinax-stripe into your app and edit models.py and admin.py files as per your requirement.

๐Ÿ‘คBiju

Leave a comment