#11707: limit_choices_to on a ForeignKey can render duplicate options in 
formfield
-------------------------------------+-------------------------------------
               Reporter:             |        Owner:  nobody
  Chris.Wesseling@…                  |    Milestone:
                 Status:  reopened   |      Version:  SVN
              Component:  Forms      |     Keywords:  ForeingKey
             Resolution:             |  limit_choices_to
           Triage Stage:  Accepted   |    Has patch:  1
    Needs documentation:  0          |  Needs tests:  0
Patch needs improvement:  1          |
-------------------------------------+-------------------------------------

Comment (by anonymous):

 Replying to [comment:11 charstring]:
 > Replying to [comment:7 lukeplant]:
 > > {{{
 > > #!CommitTicketReference repository="" revision="15791"
 > > Fixed #15559 - distinct queries introduced by [15607] cause errors
 with some custom model fields
 > > }}}
 >
 > Can someone point me to an example of such a custom model field or, even
 better, a test showing the breakage?

 The discussion linked from the description of the other ticket has an
 example. It's in dpaste so may not be long-lived. Copying here for
 reference:

 {{{
 #!python
 class PointField(models.Field):
     description = _("A geometric point")

     __metaclass__ = models.SubfieldBase

     pattern = re.compile('^\(([\d\.]+),([\d\.]+)\)$')

     def db_type(self, connection):
         if connection.settings_dict['ENGINE'] is not
 'django.db.backends.postgresql_psycopg2':
             return None

         return 'point'

     def to_python(self, value):
         if isinstance(value, tuple):
             return (float(value[0]), float(value[1]))

         if not value:
             return (0, 0)

         match = self.pattern.findall(value)[0]
         return (float(match[0]), float(match[1]))

     def get_prep_value(self, value):
         return self.to_python(value)

     def get_db_prep_value(self, value, connection, prepared=False):
         # Casts dates into the format expected by the backend
         if not prepared:
             value = self.get_prep_value(value)
         return '({0}, {1})'.format(value[0], value[1])

     def get_prep_lookup(self, lookup_type, value):
         raise TypeError('Lookup type %r not supported.' % lookup_type)

     def value_to_string(self, obj):
         value = self._get_val_from_obj(obj)
         return self.get_db_prep_value(value)
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11707#comment:12>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to