On Tue, 2010-01-05 at 04:17 -0800, bruno desthuilliers wrote:
> 
> On 5 jan, 00:05, HARRY POTTRER <cp368...@ohio.edu> wrote:
> > I have an class that I created which takes a queryset, and a field on
> > the model that the queryset represents. Like this:
> >
> > mc = MyClass(queryset=MyModel.objects.all(), lat_lng_field='lat_lng')
> >
> > The class basically filters the queryset based on some geographical
> > values, but in order to do that, it needs to know which field the
> > coordinates are stored in.
> >
> > Right now I have no choice but require you to pass in a string
> > representing the field name. But I think a better way would be to do
> > it kind of like this:
> >
> > class MyModel(models.Model):
> >     lat_lng = PointField()
> >     name = CharFIeld(max_length=20)
> >
> >     class Meta:
> >         verbose_name_plural = 'My Models'
> >         lat_lng_field = 'lat_lng'
> >
> > Then just modify MyClass to get the field name from the Model's Meta
> > class. Is this possible somehow?
> 
> Did you at least try ? It took me about 5 seconds to find out it
> wouldn't work... or at least not without a very ugly monkeypatch.
> 
> 
> > If not, whats the best way to go
> > about this without having to pass in the field name directly?
> 
> You can
> 
> 1/ store the info as a class attribute
> 
> class MyModel(models.Model):
>     lat_lng_field = 'lat_lng'
> 
>     # etc
> 
> 2/ store all "geographic" related infos in another inner class in your
> models (not worth if you only have a single info...)
> 
> 3/ add the attribute _after_ the class statement's body, ie:
> 
> 
> class MyModel(models.Model):
>    # stuff here
> 
> MyModel._meta.lat_lng_field = 'lat_lng'
> 
> 
> 4/ monkeypatch django.db.models.options to allow "lat_lng_field" in
> Meta (very bad idea IMHO)
> 
> or simply:
> 
> 5/ document the fact that the 'lat_lng' field name MUST be named
> 'lat_lng', period !-)
> 
> 
> My 2 cents...
> 

Or... :)

from django.db import models

models.options.DEFAULT_NAMES += ('my_first_val', 'my_second_val')

class MyModel(models.Model):
    class Meta:
        my_first_val = 'foo'
        my_second_val = 'bar'


Amazing, isn't it?

-- 

Jani Tiainen



--

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


Reply via email to