1👍
✅
I’m not sure I understand your question correctly, but here what I think the problems are. Reading your question, I assumed that you are a beginner, so I answered as such. Sorry if it’s not the case.
- You don’t need to add the id fields, it’s done automatically by Django because all tables need a primary key. You define a PK only when you need to name it something else than ‘id’.
- You should really read the Django tutorial which explains how to define models. User.cover_id and UserAvatar.file_id should be defined as ForeignKey. If you don’t know what a foreign key is, then stop playing with Django and read a database tutorial before.
- There’s already a model and a set of classes to manage your users in Django. You should use them. For example, a “user profile” is the right way to extend the user model.
- If you force the users to choose one avatar in a set of predefined avatars, then what you want to do is ok. If the users can choose any avatar (upload), then you should use OneToOneField or put it directly in the user model (or profile).
I don’t know what is a UserCover
, but here’s what your models could look like:
from django.contrib.auth.models import User
class UserProfile(models.Model):
# Link to Django normal User (name, email, pass)
user = models.ForeignKey(User, unique=True)
# Any information that a user needs, like cover, wathever that is, age, sexe, etc.
avatar = models.CharField(max_length=255)
Or like this if a will be reused often :
class Avatar(models.Model):
# name = ...
# description = ...
path = models.CharField(max_length=255)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
avatar = models.ForeignKey(Avatar, unique=True)
# other data
👤Nil
Source:stackexchange.com