Thursday, August 30, 2018

Re: Doubts on One-To-One Field

Actually we would be needing that information most of the time, I will have to see the which would be favourable and also of course django  has documented  a doubt I have. I guess I have to rtfd more carefully. Thank you for the information. You are a savior.



On Thu, Aug 30, 2018 at 10:54 PM Michael MacIntosh <mmacintosh@linear-systems.com> wrote:

Hello,

You are correct, creating a one-to-one relationship can potentially create more queries and hurt performance if you are not careful.  However it can also speed up certain queries if you don't need that information all of the time.

If you are worried about the performance, I would suggest looking into select_related and prefetch_related.  Select related will grab your Users and user profiles in one query, which can be faster.  Prefetch related will fire off a query for each prefetched item, so one for users and one for user profiles, which isn't useful here.

https://docs.djangoproject.com/en/2.1/ref/models/querysets/#select-related


On 8/30/2018 9:08 AM, vineeth sagar wrote:
def userProfile(models.Model):
      user=models.OneToOneField(User,primary_key=True)
       """
       Other stuff
       """
I can query it like this 
x = userProfile.objects.get(user__id=1)(Assuming this exists)

when I do a connection.queries a single query is executed.When I try to access the username i.e x.user.username a new query is executed again when I see connection.queries? Is this the expected behavior. Wouldn't this be bad if we have huge databases? I have a scenario where some classes have common information and then they have different fields based on their classes. I have tested this OneToOneField by the above example and for some reason reluctant to use this because of the new query? The other approach would be abstract base classes where information stored in the db would be duplicated but the additional query can be saved I think any thoughts?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/afb76a5c-30c7-45f0-8f8a-96e1d30e731c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
This message has been scanned for viruses and dangerous content by
E.F.A. Project, and is believed to be clean.
Click here to report this message as spam.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/84470c00-3058-3c5e-d947-1ee6e231f0b6%40linear-systems.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMMZq8OP6yAoB%3D%2BC%3Dv5bMKZuLDedLs-ORHiTk970G5DTgp9a%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment