mirror of
https://github.com/django/django.git
synced 2025-10-29 08:36:09 +00:00
[1.6.x] Fixed #22514 -- Prevented indexes on virtual fields [postgres].
Backport of 78c32f1caa from master
This commit is contained in:
committed by
Tim Graham
parent
d4a3fd44f0
commit
ef3ae3d1c9
@@ -2,10 +2,35 @@ from django.db import connection
|
||||
from django.db import models
|
||||
|
||||
|
||||
class CurrentTranslation(models.ForeignObject):
|
||||
"""
|
||||
Creates virtual relation to the translation with model cache enabled.
|
||||
"""
|
||||
# Avoid validation
|
||||
requires_unique_target = False
|
||||
|
||||
def __init__(self, to, from_fields, to_fields, **kwargs):
|
||||
# Disable reverse relation
|
||||
kwargs['related_name'] = '+'
|
||||
# Set unique to enable model cache.
|
||||
kwargs['unique'] = True
|
||||
super(CurrentTranslation, self).__init__(to, from_fields, to_fields, **kwargs)
|
||||
|
||||
|
||||
class ArticleTranslation(models.Model):
|
||||
|
||||
article = models.ForeignKey('indexes.Article')
|
||||
language = models.CharField(max_length=10, unique=True)
|
||||
content = models.TextField()
|
||||
|
||||
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateTimeField()
|
||||
|
||||
# Add virtual relation to the ArticleTranslation model.
|
||||
translation = CurrentTranslation(ArticleTranslation, ['id'], ['article'])
|
||||
|
||||
class Meta:
|
||||
index_together = [
|
||||
["headline", "pub_date"],
|
||||
|
||||
Reference in New Issue
Block a user