0

I am trying to get country where some specific post exists, so that I can show only those countries. To do so. I have following code.

Models:

class Country(models.Model):
    name=models.CharField(max_length=100)
    iso_code_2=models.CharField(max_length=4)
    iso_code_3=models.CharField(max_length=4)
    def __unicode__(self):
        return self.name
    def get_countries_with_jobs(self):
        countries=self.objects.filter(id__in=Post.country)
        return countries


class Category(models.Model):
    name=models.CharField(max_length=100)
    title=models.CharField(max_length=100)
    meta_keywords=models.CharField(max_length=100)
    meta_description=models.CharField(max_length=100)
    sort_order=models.IntegerField()
    def __unicode__(self):
        return self.name

class City(models.Model):
    name=models.CharField(max_length=100)
    title=models.CharField(max_length=100)
    meta_keywords=models.CharField(max_length=100)
    meta_description=models.CharField(max_length=100)
    sort_order=models.IntegerField()
    country=models.ForeignKey(Country)
    def __unicode__(self):
        return self.title

class Post(models.Model):
    user=models.ForeignKey(User)
    title=models.CharField(max_length=100)
    publish_date=models.DateField()
    active=models.BooleanField()
    country=models.ForeignKey(Country)
    city=ChainedForeignKey(City,chained_field="country",chained_model_field="country" )
    category=models.ForeignKey(Category)
    description=models.TextField()
    added_by=models.CharField(max_length=70)
    def __unicode__(self):
        return self.title

Here get_countries_with_jobs method have the code that is trying to query and get countries where post exist. Normally one access country of some post, but in this case I need to get countries where post exists. Here it was giving error while calling this method so I tried to write this code in view method as below.

def list(request,template_name='list_posts.html'):
     countries=Country.objects.filter(id__in=Posts.country)
     return render_to_response(template_name,locals(),context_instance=RequestContext(request))
Hafiz
  • 4,187
  • 12
  • 58
  • 111

1 Answers1

1

Your question is hard to understand, so I'll look at a couple of interpretations:

  1. You want to get countries for a particular post:

    countries = Country.objects.filter(post=post_instance)
    
  2. You want to get all countries that have any posts:

    countries = Country.objects.filter(post__isnull=False)
    

    Similarly, if you wanted to get countries that don't have a post associated with them:

    countries = Country.objects.filter(post__isnull=True)
    
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I wanted second one, should I use it in view or should I have it in country model as a function? – Hafiz Aug 16 '12 at 18:34
  • You can do either, but for such a simple filter there's not a whole lot of point in adding it to the model. – Chris Pratt Aug 16 '12 at 18:39
  • but this didn't work for me, I have a country that have post, but it don't show that using the code in option 2. – Hafiz Aug 16 '12 at 18:51
  • Update your question to include what you have now verbatim. This is a very simple query that I know works; perhaps you've just got something not quite right. – Chris Pratt Aug 16 '12 at 19:58
  • I am seding tmhis countries variable to template and accessing it in this way `{% for country in countries.object_list %} {% endfor %}` – Hafiz Aug 17 '12 at 19:32
  • 1
    There's no `object_list` attribute on `countries`. That comes from pagination, which you aren't using, and even if you were, it would just be `object_list`, not `countries.object_list`. – Chris Pratt Aug 17 '12 at 19:41
  • Thanks Chris, it is now showing countries, but repeating countries,so do I need to have some sort of groupby in this query and can we do that so that I can get one record once and do not repeat country name/id e.t.c – Hafiz Aug 17 '12 at 19:56
  • thanks Chris, using distinct for getting unique records.Thanks for your time – Hafiz Aug 18 '12 at 11:44