On May 8, 4:21 am, andre <andre.phili...@gmail.com> wrote: > """ > Background: > > As described on the "User Authentication in Django" manual, I am using > an additional model to store extra information about users. This > additional model (J2User) model uses a ForeignKey to the User model > and is specified below. > > Problems (all related): > > 1) Accessing the J2User model, how do I retrieve only one User field > for ALL records? I tried something like J2User.objects.all().username > but it didnt work. > > 2) Accessing the J2User model, how do I retrieve ALL User fields for > ALL records? > > 3) How do I write a J2User method that would return one field of the > User model? I need the implementation for something like > J2User.get_auth_user_usernames(). > > 4) How do I write a J2User method that would return ALL User fields > for ALL records? The implementation for something like > J2User.get_auth_user_fields() > > Suggestion: > > Add the answers to the above question to the documentation. > > This post also athttp://dpaste.com/hold/41999/ > """ > > # models.py > > from django.db import models > from django.contrib.auth.models import User > > class J2UserType(models.Model): > id = models.AutoField(primary_key=True) > name = models.CharField(unique=True, max_length=128) > > class J2User(models.Model): > id = models.AutoField(primary_key=True) > user = models.ForeignKey(User, unique=True) > user_type = models.ForeignKey(J2UserType) > def get_auth_user_usernames(self): > # ??? > pass > def get_auth_user_all_fields(self): > # ??? > pass >
You are failing to understand the difference between models, querysets and managers. They are all well described in the documentation, I suggest you review these concepts there first. For example, in your first question, J2User.objects.all() returns a queryset, which is a list-like collection of model instances, so it doesn't make sense to do .username on it - instead, you will have to iterate through and get the username field for each one. Alternatively, you could use the objects.values_list method, which is also well documented. Also, note that in Python it's not considered good practice to write 'getter' methods for simple attribute lookups. You can access these directly from the model instance, so there's no need for a method. -- DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---