GithubHelp home page GithubHelp logo

Comments (3)

javiermatos avatar javiermatos commented on August 25, 2024 1

Great! Thank you so much for the detailed explanation! I had it working using the recommended way so it is fine. Thank you! :)

from django-floppyforms.

rtpg avatar rtpg commented on August 25, 2024

Hi @javiermatos , sorry for the late reply on this (still figuring out the new Github notifications....)

So basically this is working "as expected". The way that Django works for this stuff when building a model form is:

  1. generate a set of fields for a model (using fields_for_model). So here this would be a call like
    opts = cls.Meta
    default_fields = fields_for_models(models.Account, opts.fields, ..., opts.widgets, ....)
    

2 - set those up on the class (so at this point default_fields will use the right widget, but it would not use the floppyforms.gis.PointField but the default Django one)

3 - override these defaults with manually declared fields. for example you declared address_location here, so that field overrides everything from fields_for_model (including stuff built up on meta)


Now, if you want to use a custom widget, you have a couple options (haven't tested any of these but I believe variants of these ideas work)

  • If you always want to use this widget for a model field, you can declare a default form field (with a default widget)
class CustomFormPointField(loppyforms.gis.PointField):
    widget = CustomPointWidget

class CustomModelPointField(PointField):  # this is a model field
    field_class = CustomFormPointField

# use the above in your models, then form fields will automatically use the right kind of field
  • If you more or less want to one-off this, you can declare form_class and widgets separately
class PointWidget(floppyforms.gis.PointWidget, floppyforms.gis.BaseOsmWidget):
    pass


class AccountForm(forms.ModelForm):

    class Meta:
        model = models.Account
        fields = '__all__'
        widgets = {
            'description': AdminTextareaWidget(),
            'services': FilteredSelectMultiple(verbose_name='services', is_stacked=False),
            'address_location': PointWidget(),  # widget declared here...
        }
        field_classes = {
           'address_location': floppyforms.gis.PointField  # custom form field declared here
       }
  • ( VERY HACKY, PLEASE USE AT YOUR OWN RISK ) If you just want all model PointFields to do the right thing, you could edit the default widget class in your settings.py or something:
class PointWidget(floppyforms.gis.PointWidget, floppyforms.gis.BaseOsmWidget):
    pass

from django.db.contrib.gis.db.models.fields import PointField as DjangoPointField

DjangoPointField.widget = PointWidget

Hope this helps

from django-floppyforms.

rtpg avatar rtpg commented on August 25, 2024

Glad to hear that you were able to get things working 😄

from django-floppyforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.