In Django, how would a database schema look like for a social media site like Instagram? For example, how would you make it so that one user can post multiple posts, and they can only have 1 profile?
I want to know what different tables I should have
I want to know how I would connect the tables
I want to know what I should write to link profile to posts(i.e. Foreign Key)
Any help is appreciated.
for profiles you add the user as a OneToOneField :
class Profile(models.model):
User=models.OneToOneField ()
Phone = models.CharField(default='', max_length=20)
Zip_code=models.IntegerField(default='')
image = models.ImageField(blank=True,upload_to='users_photos',)
and for post you add the user as a foreign key:
class Post(models.Model):
User=models.ForeignKey(
User,
on_delete=models.CASCADE,
)
....and some other fields you want to add
Related
I want to create a Relational database with auth_user table which is automatically created in django after migrations(database is postgresql). I have created a table with class named order. My models.py looks like this
Models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class order(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.CharField(max_length=50)
price = models.IntegerField()
now I want to link the user id with table order with user_id. And my views.py looks like this.
views.py
def order_Data(request):
product = request.POST['product']
price = request.POST['price']
orders = order(product=product,price=price)
orders.save()
messages.info(request,'Data saved')
return render(request,'home.html')
I think I have linked tables properly but in user_id of order table is setting as null and giving me error. I dont know what to do now. Please Help me out (Iam also a learner in Django). Thank you in advance.
You didn’t pass the user to the order
orders = order(product=product,price=price, user=request.user)
Given you have a logged-in user
I have 2 database tables, Prospects and Profile. They're related by a One-to-one foreign key relationship
Model.py
class Prospect(models.Model):
profile = models.OneToOneField(Profile, on_delete=models.CASCADE, null=True, blank=True, related_name="profile_prospects")
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
In my view.py
prospects = prospects[:50]
I have a QuerySet of prospects (prospects is working correctly, exactly what I want), and I would like to retrieve a QuerySet of profiles based on the database model above. I tried
profiles = Profile.objects.filter(profile_prospects__in = prospects)
It returns an error of
django.db.utils.ProgrammingError: subquery has too many columns
How can I get all the relevant profiles?
You have spaces in
profiles = Profile.objects.filter(profile_prospects__in = prospects)
Sorry, I might be confused here. But isn't the profile automatically inherited by the prospect since it's a one-to-one relationship?
When you have the prospect you should be able to get the profile like this
prospect.profile
Again, I might have gotten the question wrong.
I have managed to add an additional field to the Registration form, "where did you hear about us?".
But I am not sure which files to edit in order to store the data from this field along with the users info.
i.e. When logging into the Admin section and go to "users" and view a users info I would like to see this field there.
Simplest way would be to store additional data in a UserProfile model about the user, e.g.
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
where_heard_about_us = models.TextField()
You can then register the object as an inline object in your Django Admin
I have a profile table which have a foreign key of the user.
class Profile(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
image = models.CharField(max_length=100)
user = models.ForeignKey(User)
I also have a COMMENT table which have a foreign key of the user.
class Comment(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
content = models.TextField()
user = models.ForeignKey(User)
I want to query the table COMMENT and also I want to get the image of the user which is in the PROFILE table. How can I query this in most effective way in django?.
thanks
If you are okay with changing ForeignKey to OneToOneField on Profile model then you can do it like,
Comment.objects.all().select_related('user__profile')
The above one selecting additional related-object data when it executes its query. This is a performance booster which results in a single more complex query but means later use of foreign-key relationships won’t require database queries.
Otherwise you can get it this way
for comment in Comment.objects.all():
print comment.user.profile_set.all()[0].image
If you're using Django = <1.4 the following is best practice:
comment = Comment.objects.get(pk=1)
comment.user.get_profile().image
Following the deprecation of native Profile model support (Django 1.5+) the following is still possible:
comment = Comment.objects.get(pk=1)
comment.user.profile.image
Django 1.5+ introduces custom auth models so you can do the following:
comment = Comment.objects.get(pk=1)
comment.user.image
At very least you should change your ForeignKey to a OneToOne relation for the user column on Profile, as Django =< 1.4 expects only one User Profile to be associated with a User.
References:
Django 1.4 get_profile: https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.models.User.get_profile
Django 1.5+ extending the user model:
https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model
So , i have a django project with 2 apps
├───Blog
post
comment
└───User
profile
user (basic authentification and profile dashboards)
blog (having basic blog models : post,comment)
I need to give the users the possbility to create one or more blogs within the same project, but i don't see how i can treat an app like a model.
The only solution i figured out , is to add a foreign key to user id for every model withn the blog app.but is there a better way ?
I think you should do that:
# blog/models.py
class Blog(Model):
owner = ForeignKey(User, related_name="blogs")
name = Charfield()
class Post(Model):
blog = ForeignKey(Blog, related_name="posts")
#Other fields ...
class Comment(Model):
post = ForeignKey(Post, related_name="comments")
#Other fields ...