Divino
Divino

Reputation: 47

Store List(s) in a database

My Users has phone contact LIST(s) - [3121234567,2121234567,6601234567]

Now, I want each user to be able to store as many LIST as possible. Each List must have a name(or description) attached to them under each USER account. Note: the number of LIST is dependent on Users needs. Example:

Students [3121234567,2121234567,6601234567]

Club Member [8101234567,8151234567,8171234567]

Now, how do I store it in a database.

Django User Model

class CustomUser(AbstractBaseUser):

username = models.CharField(max_length=254, unique=True)    
first_name = models.CharField(max_length=24)
last_name = models.CharField(max_length=30)
phone = models.CharField(max_length=10)
email = models.EmailField(max_length=128)
street = models.CharField(max_length=128)
city = models.CharField(max_length=128)
state = models.CharField(max_length=2, choices=STATE_CHOICES, default=STATE)
zip_code = models.IntegerField(max_length=5, null=True, blank=True

USERNAME_FIELD = 'email'
REQUIRED_FIELD = ['username', 'first_name', 'last_name', 'phone', 'street', 'city', 'state']

objects = CustomUserManager()

Edit(Added): I am not looking to create Student or ClubMember models. This name is use to identify the python phone contact list. PhoneAddress one can be labelled(named) as Student for one user but called "Conference Attendant" for another. Each user have different # of Lists.

PhoneAdress PhoneAdress2 PhoneAdress3 [3121234567,2121234567,6601234567] [4121234567,3121234567,6601234567] [7121234567,8121234567,9601234567]

Upvotes: 1

Views: 6310

Answers (2)

dmitryro
dmitryro

Reputation: 3506

Create some models:

 class Club(models.Model):
     name = models.CharField(max_length=256,blank=True,null=True)
     date_open = models.DateField()

 class Institution(models.Model):
     name = models.CharField(max_length=256,blank=True,null=True)
     address = models.CharField(max_length=256,blank=True,null=True)
     type = models.CharField(max_length=256,blank=True,null=True) #university, college

Rather than using

 class CustomUser(AbstractBaseUser):
     username = models.CharField(max_length=254, unique=True)    
     first_name = models.CharField(max_length=24)
     last_name = models.CharField(max_length=30)

use composition in the form of OneOnOneField

 class UserProfile(models.Model):
     user = models.OneOnOneField(User,blank=True,null=True)
     club_member = models.ManyToManyField(Club,blank=True, null=True)
     institutions = models.ManyToManyField(Institution,blank=True, null=True) # student in

Once you have this, you will be able to get and add as many institutions and clubs to the lists:

user = User.objects.get(id=user_id)
club = Club.objects.get(id=club_id)
institution = Institution.objects.get(id=institution_id)

user.profile.clubs.add(club)
user.profile.institutions.add(institution)

So to verify if the user is a member of a club

club = user.proile.clubs.get(id=club_id)

and to verify the user is a student in an institution use

institution = user.profile.institutions.get(id=institution_id)

Upvotes: 0

KhoPhi
KhoPhi

Reputation: 9517

Lemme guess, you're coming from a NoSQL background where the database is a document in a JSON form?

If so, I am sorry, in a Relational Database, used by Django in the likes of PostgreSQL or MySQL, they call something Foreign Keys, and that is your way of storing multiple "Lists" related to a particular field.

If you want many users to store as many lists as possible, you're looking at something like this, roughly speaking:

class myUserModel(models.Model):
    # your arbitrary fields here
    # then you do something like this:

class Student(models.Model):
    user = models.ForeignKey(User)

class clubMember(models.Model):
    user = models.ForeignKey(User)

With the above setup, you can add as many Student objects associated to the myUserModel class, so as the clubMember

However, if you wish to use PostgreSQL specifically, as your backend (perhaps as perpetual storage backend), you might find some sense in using Django's support for the ArrayField

And ooh, you might need to extend the Django User Model to add any extra fields easily, unless you're willing to go down the road of a custom User Model.

More info:

I hope the above helps

Upvotes: 3

Related Questions