Reputation: 3554
I've got a model named String that looks something like this:
class String(models.Model):
text = models.TextField()
language = models.ForeignKey(Language, default=get_english)
original_string = models.ForeignKey('self', null=True, blank=True)
The default language for the strings is English, then, in the same table on the database, I have the translated strings with different languages, each one pointing to the corresponding string in English through original_string
.
What I need is to retrieve the strings in English that DON'T have associated strings in another language, i.e., that don't have a translation.
Right now I'm iterating over all string in English and appending the ones I need to a list, like:
translatable_strings = String.objects.filter(language__name="English")
strings = []
for string in translatable_strings:
if not String.objects.filter(language=translator_lang,
original_string=string).exists():
strings.append(string)
But I think that's a pretty nasty piece of code. Isn't there a way to make this in a single query?
Upvotes: 3
Views: 164
Reputation: 39659
Try this
String.objects.filter(language__name="English", original_string__isnull=True)
Upvotes: 1
Reputation: 51655
First condition: language is english
Second condition: not exists another string that references it.
String.objects.filter( language = 'english',
string__original_string__isnull = True )
A foreignkey backward reference is needed.
Upvotes: 4