I was following this articles to have 2 columns per 1 field , so my custom field code is something like this :
class GeopositionField(models.Field):
description = "A geoposition (latitude and longitude)"
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 42
super(GeopositionField, self).__init__(*args, **kwargs)
def contribute_to_class(self, cls, name):
self.name = name
position_longitude = DecimalField(decimal_places=6,max_digits=9,default=0,blank=True)
cls.add_to_class("position_longitude",position_longitude)
position_latitude = DecimalField(decimal_places=6,max_digits=8,default=0,blank=True)
cls.add_to_class("position_latitude",position_latitude)
setattr(cls,"position_longitude",position_longitude)
setattr(cls,"position_latitude",position_latitude)
And my model class is Request :
class Request(models.Model):
person = models.ForeignKey(Person)
position = GeopositionField(null = False,default = 0)
( I'm modifying django-geoposition ) Until now I have only position_latitude and position_longitude in my table (and not only "position" like it was originally)
Before
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| person_id | int(11) | NO | MUL | NULL | |
| creation_date | datetime | NO | | NULL | |
| position | varchar(50) | NO | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
After
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| person_id | int(11) | NO | MUL | NULL | |
| creation_date | datetime | NO | | NULL | |
| position_longitude | decimal(9,6) | NO | | NULL | |
| position_latitude | decimal(8,6) | NO | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
That's good, but the problem comes in django admin, and also in the shell, because if I create a "Request" object and then I try to print "position" attribute, I got a error that says the "position" attribute doesn't exists :
>>> from main.models import Request
>>> x = Request()
>>> x.position
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Request' object has no attribute 'position'
>>>
It works if I set position attribute in __init__
method of Request class, but that's not the idea.
So, back to the real problem, when I try to show it in admin panel, if I call explicitly to "position" field django throws an error, curiously it works with "position_latitude" and "position_longitude"
class RequestAdminForm(forms.ModelForm):
class Meta:
model = Request
# fields = ['position_latitude','position_longitude] <-- this works !
fields = ['position'] # <-- this returns error = Unknown field(s) (position) specified for Request
Is there a way to show "position_latitude" and "position_longitude" when RequestAdminForm only have "position" in fields list? That's what I want to achieve. Why happen the "undefined-attribute" problem ?