1👍
Easiest and most efficient way is to filter first:
blog.entry_set.filter(published=True)
Or, you can simply use an if statement:
if entry.published:
print entry
0👍
for o in queryset:
if not results.has_key(o.name):
results[o.name] = list()
if not (...check if Z):
results[o.name].append(o)
for a in results:
print a
for b in results[a]:
print b.state
Suppose you have models:
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
def __unicode__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateTimeField()
mod_date = models.DateTimeField()
authors = models.ManyToManyField(Author)
n_comments = models.IntegerField()
n_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __unicode__(self):
return self.headline
and you want to exclude all blogs that have name “test”
You would run the code:
Entry.objects.exclude(blog__name=’test’)
and it would write query:
SELECT ... FROM `entry` INNER JOIN `blog` ON (`entry`.`blog_id` = `blog`.`id`) WHERE NOT (`blog`.`name` = test )
0👍
aobjs = [b.a for b in B.objects.exclude(state='Z')]
Or
aobj.b_set.exclude(state='Z')
- [Answer]-How can I perform this query?
- [Answer]-How to save a local file in a FileField in django
- [Answer]-How to store tables using jquery ajax django
- [Answer]-Remove first 3 characters from a field in the database
- [Answer]-Django – difficult order by (two M2M relations)
0👍
Change Blog.objects.all()
with Blog.objects.filter(entry__published=True)
That should do the trick!
Source:stackexchange.com