Why some Django fields are not saved in database?

56 views
Skip to first unread message

prateek gupta

unread,
Jun 21, 2018, 4:42:09 AM6/21/18
to Django users
Hi All,

I have a store address form in Django which have fields like- name, address, location, latitude, longitude etc.

I am autofilling the latitude, longitude fields based on address with the help of django package-https://github.com/caioariede/django-location-field


In django view after the form submisson I am able to see both fields values-



Issue is in database , latitude/longitude values are blank.

What I am doing wrong here?


models.py-

from location_field.models.plain import PlainLocationField
class Store(OwnedModel):
  address = models.TextField(default='Mumbai')
  location = PlainLocationField(based_fields=['address'], zoom=7, null=True)

  @property
  def latitude(self):
      if not self.location:
          return
      latitude, _ = self.location.split(',')
      return latitude

  @property
  def longitude(self):
      if not self.location:
          return
      _, longitude = self.location.split(',')
      return longitude

  class Meta:
      managed = False
      db_table = 'store'

admin.py-

class StoreAdmin(admin.ModelAdmin):
  list_display = ( 'address', 'latitude','longitude')
  readonly_fields = ('latitude', 'longitude',)


I am using Django2.0.6,Python3.6,Mysql

Melvyn Sopacua

unread,
Jun 21, 2018, 5:18:46 AM6/21/18
to django...@googlegroups.com
On donderdag 21 juni 2018 10:42:09 CEST prateek gupta wrote:

> Issue is in database , latitude/longitude values are blank.
>
> What I am doing wrong here?

Why do you not understand your own code? How did you get that code - you
explicitly code it so that it they do NOT end up in the database. So why are
you asking why?

--
Melvyn Sopacua

prateek gupta

unread,
Jun 21, 2018, 5:31:26 AM6/21/18
to Django users
Thanks for reviewing the code.

I am new to Django and just tried to put logic from googling to achieve my goal.
That's why I don't understand my code completely.

Can you please point out where I am doing this and how can I change this behaviour?

Melvyn Sopacua

unread,
Jun 21, 2018, 8:25:42 AM6/21/18
to django...@googlegroups.com

On donderdag 21 juni 2018 11:31:26 CEST prateek gupta wrote:

 

> Thanks for reviewing the code.

>

> I am new to Django and just tried to put logic from googling to achieve my

> goal.

> That's why I don't understand my code completely.

 

Try to understand it before applying. The main issue you're facing is the difference between class attributes and model fields.

 

A model field is a special class attribute that Django stores in the database. You have to declare them using one of the field types that Django supports.

Everything else is not saved in the database.

 

You're using a custom model field for the location in your model. Part of that field is that it stores in the lon/lat information for you, so that you don't have to worry about it. What it doesn't provide is a solid way for you to get the location data from the field. As you've figured out, it is stored as a string, which makes it difficult to make queries on lon/lat coordinates.

 

So it's probably not the right tool for the job. But before you go looking for a different package, you should really learn more about Django, for example complete the tutorial and in your case it'll be helpful to go through the GIS tutorial as well. Cause then you'd discover that other then support for Mapbox and Google maps, Django already has built in most of what the DJango Location Field package is offering you.

 

--

Melvyn Sopacua

prateek gupta

unread,
Jun 21, 2018, 8:28:32 AM6/21/18
to Django users
Thanks for sharing this, I will do the same first.
Reply all
Reply to author
Forward
0 new messages