I try to get person id of my Person model, connected with User model
from django.db import models
from django.contrib.auth.models import User
class Person(models.Model):
user = models.ForeignKey(User, blank=False, null=False, on_delete=models.CASCADE)
middle_name = models.CharField(max_length=30)
def __str__(self):
return '%s %s %s' % (self.user.last_name, self.user.first_name, self.middle_name)
from template-generated page, but I only can get user id who authenticated right now
{{ user.id }}
but not Person id.
How can I get Person id through user id on the page?
django automatic reverse lookup foreign key (user field) from an model, if you want to get person id try this {{user.person.id}}
I suggest OneToOneField for this kind of relation.
user = models.OneToOneField(User, blank=False, null=False, on_delete=models.CASCADE)
then you can use user.person
I think you are having logical issue with your model. One person generally should have connected with one user so you should user OneToOneField instead of ForeignKey. Let's think you are correct then you can follow the code below, if you are not then it will work too.
You can get it through context variable
Suppose your person=Person.obejcts.get(id=1)
Then pass it through context in view like
def view(request):
person=Person.obejcts.get(id=1)
context = { 'person':person}
return render(request, 'your.html', context}
And in template you can use {{person.id}}
You can simply do {{ user.person_set.get(pk=1) }} . This assumes you know the pk...if you only actually store 1 person with 1 user, then substitute the .get() with a .first() ...you can access your foreign key model using the main_obj.fk_obj_set.all() and do things just like any other queryset. This obviously assumes you have multiple users for 1 person..otherwise things get easier with a 1:1 field.
Related
Basically, I am working on a django project, and whenever I insert data into the database, the result is weirdly formatted.
this is my model
customer.py
class Customer(models.Model):
user = models.OneToOneField(User,null=True,blank=True,on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
email= models.CharField(max_length=200, null=True)
phone_number= models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
Now, say I have saved a new customer
new_customer = Customer.objects.create(name="Henry",email="henry#mail.com",phone_number="+330145786259")
new_customer.save()
when i try to retrieve the customer name i get this:
print(new_customer.name)
>('henry',)
Anyone has any insight for me???
I tried to recreate the model on a new project but still having the same result
In your customer class, you have defined a 1:1 relationship with the in-built user model class of django. And when you are creating the customer object, new_customer, you have not specified the 'user' attribute; hence, your customer object is missing a key element.
The user object already has an in-built field for storing names. It is 'first_name' and 'last_name.' You need to create a user model first before being able to create your 'Customer' model.
Your models.py should look something like this:
from django.contrib.auth.models import User
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
phone_number= models.CharField(max_length=200, null=True)
def __str__(self):
return self.user.first_name
# to return email -> self.user.email
Now to create a 'Customer' object in view.py:
from django.contrib.auth.models import User
from .models import Customer
# create a user object
myuser = User.objects.create_user(username='john', email='jlennon#beatles.com', password='glass onion')
# pass the user object to the customer model
mycustomer = Customer.objects.create(user=myuser, phone_number=123456789)
# save the customer object
mycustomer.save()
Explore django ModelForms to define the user model as per your specifications, e.g, if you don't require your users to have passwords associated with them, etc.
After much testing, I realized why I was getting the weird output.
I was directly passing data from a form to the object creation method, like so:
data = json.loads(request.body)
new_customer = Customer.objects.create(name=data['name'],email="henry#mail.com",phone_number="+330145786259")
new_customer.save()
So assigning the received data to a variable before passing it to the object creation method seems to be the right way of doing things... At least, it is working for me.
I have an app within my project called posts, where inside their in the models.py, I have two models: Post and Like.
I want to add a many-to-many-field on the post that references the Like model.
I have executed makemigrations and migrate, however I am getting this error:
NameError: name 'Like' is not defined
models.py:
class Post(models.Model):
file = models.ImageField(upload_to='images/')
summary = models.TextField(max_length=600)
pub_date = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
likes = models.ManyToManyField(Like)
def __str__(self):
return self.user.username
def summary_pretty(self):
return self.summary[:50]
def pub_date_pretty(self):
return self.pub_date.strftime('%b %e %Y')
class Like(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.BooleanField(default=False)
it says
NameError: name 'Like' is not defined
because you must move Like model above Post model it hasn't been defined yet in your code in that line (python code is interpreted)
and it's not a good thing to design database models like that based on:
Why should I avoid loops when designing relationships for a database?
so delete the Post foreign key in Like model and you can retrieve a like's post with reverse lookup
you can find more about it in django official docs:
Lookups that span relationships
and also
Many-to-many relationships
You're referencing the class Like before initiating it in your Python file. Hence "Like" is not defined.
likes = models.ManyToManyField(Like)
You need to delete "likes" from your Post class. That will fix the error.
As for your code, I think you're misunderstanding why we use Intermediary Tables. You don't need to reference Like in your Post class. You've already established that relationship in the Like Class:
post = models.ForeignKey(Post, on_delete=models.CASCADE)
Retrieving the users who have liked a post is as simple as writing a filter on the Likes table.
#views.py
from models import Post, Like
post_id = 1
likes = Like.objects.filter(post=post_id)
i created a model
class ThisUser(models.Model):
created = models.DateTimeField(auto_now=True)
message = models.CharField(max_length=120)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.user
I want to store message specifically for the user who is authenticated.
right now this will give me all user who is available in my user model.
Please help
user = models.OneToOneField(User, on_delete=models.CASCADE)
Instead of foriegn key use one to one relation
Well you are suppose to take care of that at view level and not model level,
for example this is how I create a new Album:
#login_required
def make_album(request):
if request.method == 'POST':
form = AlbumCreationForm(request.POST)
if form.is_valid():
new_album = core_models.Album(name=form.cleaned_data['name'], description=form.cleaned_data['description'], user=request.user)`
You can use request.user.id to get user's id for further use.
I have a profile model which refers to the default User model of Django. I want a list of users with increasing points.
My Profile Model:
class Profile(models.Model):
username = models.ForeignKey(User, on_delete=None, primary_key=True)
image = models.CharField(max_length=255, default="")
points = models.IntegerField(default=0)
views = models.IntegerField(default=0)
def __str__(self):
return self.username.username
I am firing this query:
users = User.objects.filter(username__in=Profile.objects.filter().order_by('-points').values('username__username'))
print(users,Profile.objects.filter().order_by('-points').values_list('username__username'))
And my output is
<QuerySet [<User: aplha>, <User: baqir>, <User: beta>]> <QuerySet [{'username__username': 'beta'}, {'username__username': 'baqir'}, {'username__username': 'aplha'}]>
Now here we see, Profile model is giving the usernames in the same order that I want to, but the user model is not maintain that order. How to I make it maintain the same order as the Profile returns.
How about this:
from django.db.models import Sum
users = User.objects.annotate(points=Sum('profile__points')).order_by('-points')
This is untested but I hope it helps!
When you use _in, there is not guarantee of the order of the results.
You could fetch the list of users by looping through the profiles. If you use select_related() then you will avoid extra queries to fetch each user.
profiles = Profile.objects.filter().order_by('-points').select_related('username')
users = [p.username for p in profiles]
Note that it might be better to rename the foreign username foreign key to user, since following the foreign key returns a User instance, not the username string.
I'm using django's built-in contrib.auth module and have setup a foreign key relationship to a User for when a 'post' is added:
class Post(models.Model):
owner = models.ForeignKey('User')
# ... etc.
Now when it comes to actually adding the Post, I'm not sure what to supply in the owner field before calling save(). I expected something like an id in user's session but I noticed User does not have a user_id or id attribute. What data is it that I should be pulling from the user's authenticated session to populate the owner field with? I've tried to see what's going on in the database table but not too clued up on the sqlite setup yet.
Thanks for any help...
You want to provide a "User" object. I.e. the same kind of thing you'd get from User.objects.get(pk=13).
If you're using the authentication components of Django, the user is also attached to the request object, and you can use it directly from within your view code:
request.user
If the user isn't authenticated, then Django will return an instance of django.contrib.auth.models.AnonymousUser. (per http://docs.djangoproject.com/en/dev/ref/request-response/#attributes)
Requirements --> Django 3, python 3
1) For add username to owner = models.ForeignKey('User') for save that, in the first step you must add from django.conf import settings above models.py and edit owner = models.ForeignKey('User') to this sample:
class Post(models.Model):
slug = models.SlugField(max_length=100, unique=True, null=True, allow_unicode=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
2) And for show detail Post, special owner name or family or username under the post, you must add the following code in the second step in views.py:
from django.shortcuts import get_object_or_404
.
.
.
def detailPost(request,slug=None):
instance = get_object_or_404(Post, slug=slug)
context = {
'instance': instance,
}
return render(request, template_name='detail_post.html', context=context)
3) And in the third step, you must add the following code for show user information like user full name that creates a post:
<p class="font-small grey-text">Auther: {{ instance.owner.get_full_name }} </p>
now if you want to use user name, you can use {{ instance.owner.get_username }}
or if you want to access short name, you can use {{ instance.owner.get_short_name }}.
See this link for more information.