Git Product home page Git Product logo

Comments (5)

furious-luke avatar furious-luke commented on July 19, 2024

Hi there, have you tried rendering the field like this: {{ form.address_field }}? The Django documentation recommends rendering fields without .value.

from django-address.

furious-luke avatar furious-luke commented on July 19, 2024

Oh hang, on I understand what you're after; you probably want just the value without the input tags in order to do some custom rendering. Yep, you'll be getting the PK because the AddressField is inherits directly from ForeignKey. Give me a minute and I'll hunt down a way to get the rendered string instead of the PK.

from django-address.

furious-luke avatar furious-luke commented on July 19, 2024

Okay, so, after a little investigation it seems getting the PK back from a call to value is the expected behaviour. This makes sense, as the value in the case of models is the PK (there could be a number of models with the same string representation but different PKs).

To get a string representation of the address from the form field you can access the model object from the initial data: {{ form.initial.address_field }}. I'm not sure if this is the best way to do it, but it should work.

from django-address.

macmichael01 avatar macmichael01 commented on July 19, 2024

I tried your suggestion but it still returns a pk.

from django-address.

furious-luke avatar furious-luke commented on July 19, 2024

I've modified the example site in example_site to show you a working implementation of my suggestion. I'm guessing you might be setting the initial value to a PK instead of an Address object if it still gives you a PK from the initial dictionary.

In any case, as I said, getting a PK from value is expected behaviour from Django's ModelChoiceField. There are other ways of getting a string representation of the field, see here for an example. As such, this is not actually an issue with django-address, so I'll close this issue.

from django-address.

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.