Search code examples
pythondjangodjango-1.10

Updating user information from separate model Django


I'm creating a simple weight management application where users can register, login and update information such as weight, body measurements etc. I've not used Django for a little while and slowly learning best practices from where i left off a little while ago.

I'm using the django-allauth to manage the user registration as this allows people to login with Facebook etc.

I've created a simple app called 'Stats' with a ForignKey to the Users.

class Stat(models.Model):

    user = models.ForeignKey(User, default=False)

    height = models.CharField(max_length=20)
    weight = models.CharField(max_length=20)

    waist = models.CharField(max_length=20)
    hips = models.CharField(max_length=20)
    upperleg = models.CharField(max_length=20)

    upperleg = models.CharField(max_length=20)
    calf = models.CharField(max_length=20)

    bodyfat = models.CharField(max_length=20)

What i would like the user to be able to do is login and update stats on a daily / weekly basis. Then to be able review previous stats. This will probably be done via a model form based on the above approach. I will add more complexity as time goes on.

Is there another way to do that would have any advantages? If the best approach is the best? Is there a way i can list all the objects from that model inside the users page in the admin to be able to reviews users stats easily enough?

Thanks in advance.


Solution

  • It is recommended that you create a separate user profile model for app-specific functionality that relates to a User of your app. It's best to create a UserProfile model which has a OneToOneField link to User object. Then, your Stat model can ForeignKey to UserProfile instead of User. This is to provide abstraction to the User object and give flexibility if you decide to create another Django app in the same project that requires different specifications for the user. Also, with UserProfile you can add customised fields that are app specific.

    Another thing you can do to improve this modelling is create separate entities for each stat. For example, HeightWeightStat, WaistStat. But, this is not completely necessary and totally depends on your preference.

    To display all of the user related Stat instances in the admin page, use Django Admin inlines: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#inlinemodeladmin-objects