Select * from users,country where country.id = users.id in Django using amin.contrib

45 views
Skip to first unread message

django noob

unread,
Jun 18, 2012, 8:13:50 PM6/18/12
to Django users
Greetings Django Experts!

Say for example i have the following on my models.py


class Country(models.Model):
name = models.CharField(max_length=50)

def __unicode__(self):
return u'%s %s' % (self.name, self.ID)

class Meta:
verbose_name = 'Countries'

class Users(models.Model):
name = models.CharField(max_length=50)
cUsers = models.ForeignKey(Country)

def __unicode__(self):
return self.name

class Meta:
verbose_name = 'Users on a country'

class GoalsinCountry(models.Model):
gCountry = models.ForeignKey(Country)
name = models.CharField(max_length=50)
descr = models.TextField(blank=True, null=True)

def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Goals Topic'



How would you configure this in GoalsinCountry to return only users in
a particular country and save this to
GoalsinCountry Table?

if i use gCountry = models.ForeignKey(Users)
I would get a list of all users, i am interested in fitering to users
on a particular country??

Please advice

Thank you

django noob

unread,
Jun 22, 2012, 3:28:31 PM6/22/12
to django...@googlegroups.com
Bump!

Kurtis Mullins

unread,
Jun 22, 2012, 3:50:23 PM6/22/12
to django...@googlegroups.com
Hey,

Here's some sample code just from reading what you've provided.

from django.contrib.auth.models import User
users_in_country = User.objects.filter(cUser__name="some_country_name")
if bool(users_in_country): # Check to make sure there's at least one User in the Country
    for user in users:
        goal = GoalsinCountry.objects.create(gCountry=user.cUsers, name="Some Name", descr="Some Description")
        goal.save() # Possibly not needed, I'd have to run the code to check

With that said, I'd recommend changing the naming conventions of your Model fields. For example:

User
- name
- country

Goals
- name
- description
- country

Country
- name

Bump!
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/6Ekb7926kzkJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Kurtis Mullins

unread,
Jun 22, 2012, 3:54:09 PM6/22/12
to django...@googlegroups.com

With that said, I'd recommend changing the naming conventions of your Model fields. For example:

User
- name
- country

Goals
- name
- description
- country

Country
- name

Sorry, wanted to mention a small fix on what I imagine would be better naming conventions (just so your code is clearer and easier to query)

Goals
- name # Is this the user's name? Then why not just make this a foreign key to a User? so maybe user = models.ForeignKey(User) 


Reply all
Reply to author
Forward
0 new messages