Hi.

You can add fields dynamically on model __init__ method and AFAIK that's
best and recommended way to do it.

I'm not sure is that even in Django docs.


ke 10. heinäk. 2019 klo 17.44 אורי <u...@speedy.net> kirjoitti:

> Hi,
>
> I have a ModelForm with Django 1.11, and I'm moving a few fields to
> another model. Calling make_migrations causes an error because these fields
> don't exist in the current model. I added some of the fields to the form,
> but one of the fields is a TranslatedField and therefore there are
> currently 2 fields, and in the future there might be more, depending on the
> number of languages. The name of the field is city, and currently I get an
> error message "Unknown field(s) (city_en, city_he) specified for
> SiteProfile" (because I'm using 2 languages - "en" and "he") - but I want
> to create all the fields dynamically with a for loop over the languages we
> use in the project. Can I override (and is it a good programming method)
> the __new__ method or is there another way? I prefer not to hard-code the
> specific field names (city_en and city_he) because they may change in the
> future, depending on how many languages we use.
>
> You can see my current commit (not working) on GitHub:
>
> https://github.com/speedy-net/speedy-net/commit/f2c9b306c1a5f7a41b19d605f7871c07a67af93f
>
>
> And the current code of this branch:
>
> https://github.com/speedy-net/speedy-net/blob/uri_move_fields_to_speedy_net_2019-07-10_b/speedy/match/accounts/forms.py
>
>
> I would like to know what is the best programming method to define a
> dynamic list of fields (which are all identical, and only one of them will
> be used, the other are removed in the __init__ method) in a ModelForm where
> the fields are saved in another model (there are 2 models but only one
> form).
>
> I still didn't commit the migrations because of this error when running
> make_migrations.
>
> (I defined a command make_migrations which only does makemigrations)
>
> Thanks!
> אורי
> u...@speedy.net
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABD5YeFv7HFCmFqvQeg5HdQLBs%3DYbxbRO4oukpdFX175exNSHA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CABD5YeFv7HFCmFqvQeg5HdQLBs%3DYbxbRO4oukpdFX175exNSHA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91odauAH%2B2Jnjdb7Yx61juKqmo3h2M05B3uNc1NDig7D9UA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to