model - New to extending Django user profiles, Need help creating a contacts/phone numbers list for each user in the database -
i'm bit new django , far been working simple database/model configurations. i'm beginning run against limitations. need add list of contacts individual users stores collection of names , phone numbers. see no way of doing userprofile model have created other storing whole list string or deserialized json in single char or text field (yuck).
for example have model:
class userprofile(models.model): user = models.onetoonefield(user) #persional informaiton height = models.charfield(max_length=24, blank=false,null=true) weight = models.charfield(max_length=24, blank=false,null=true) birthday = models.charfield(max_length=24, blank=false,null=true) ###this field need add!! #contacts contact_list = models.somekindoffield() #could contain either array or json?? class meta: managed = true db_table = 'user_profiles' user.profile = property(lambda u: userprofile.objects.get_or_create(user=u)[0])
are there other ways of doing this? have thought of creating table each user "name" , "phone" columns started wonder if nightmare manage.
i doing little research using models.foreignkey(user) never quite grasped implementation of it.
any pointers here appreciated! thanks.
if you're looking in standard relational db way, there few options.
- add
contact
model name , phone number attributes , foreignkeyuserprofile
. allowuser_profile.contact_set.all()
. or instead of fk use manytomany if want multiple users able share contacts. - if want phone numbers associated user or user profile records, user's contact list other users instead of assorted phone numbers , names, add
phone_number
attribute userprofile model.contact_list = models.manytomanyfield('self')
. see docs: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.manytomanyfield.symmetrical
alternatively, if don't want worry more model relationships , you're using postgres, can use hstore
, allows store dictionary fields. see https://github.com/djangonauts/django-hstore actual documentation on how works.
Comments
Post a Comment