Django: One FormWizard for multiple models - django

Would it be possible/best practice to use 1 FormWizard for manipulating multiple models?
I've experimented with the FormWizard and have defined all the forms. The page 'flow' itself works like a charm. However with all the checks that need to be done and Models that are manipulated it feels like I'm sticking code in the form's __init__ and/or process_step() that really belongs in views.py. The docs even state: "Dont manipulate form data via process_step().
Testing a concept with everything in one view, thus using a last_submitted_page (the step equiv) it feels like I'm writing another formwizard.
Anybody been here before? Tips welcome.
Thanx a lot.
Regards,
Gerard.

Zooming out on the issue I decided to go for the FormWizard approach. I re-confirmed that code is easily placed in forms.py and that you can get your model info by using. the process_step().
GrtzG

Related

how can i replicate admin.TabularInline outside of the admin (on the user side?)

Given a mode A and a model B that has a field with a many to many relationship with model A, I am trying to allow users creating an object of model B to also create an object of model A inline/on-the-fly just like TabularInline allows you to do on the admin.
This is a very common problem and the solution is not trivial (at least for the moment). Django admin uses Javascript(jQuery) to do this task. Multiplication of a form requires lots of altering values and IDs etc. But recently people started doing this with htmx. The way it is done is explained in this article from JustDjango thoroughly. There is even a video tutorial about it. I personally like the way they do it. You can give it a try. It feels and looks like in the django admin. If you would like to do it purely in django, you can look up formset_factory

Django nested formsets snag

As far as I'm aware, even as of Django 1.5, there's no built-in handling of nested formsets - i.e. I have an arbitrary number of groups, to which I must add an arbitrary number of members, all from the same page. I'm currently trying to use Nathan Yergler's method to do so, but it seems to be broken under Django 1.5.
The gist of the method is to override the group formset's add_fields method to include an inline_formset of members. However, when I create a GroupFormSet instance in the view, regardless of whether I've passed any group instances, I get a ValidationError: 'ManagementForm data is missing or has been tampered with'
For example, a snippet from my view's get_context_data:
group_inst = models.TemplateFieldGroup.objects.filter(name="Study")[0]
context['group_formset'] = forms.GroupFormSet(instance=group_inst)
Has anyone successfully deployed this method under Django 1.5, or perhaps does anyone have a better way to accomplish the same goal?
Slight edit: my 'groups' are actually members of an even larger umbrella: a Template object has multiple TemplateFieldGroups which have multiple TemplateFields. However, even passing GroupFormSet() a proper Template instance doesn't solve the issue at hand.
I had a similar issue recently. I solved it by creating some custom form and formset classes based on the StackOverflow answer from: Django admin - inline inlines (or, three model editing at once)
This method worked well for Django 1.4 but stopped working when I updated to Django 1.5. To solve it I created a github repository: https://github.com/didorothy/mlrma
The README.md explains my specific situation and goals more fully. My solution is focused on the Django admin but could be pieced apart and used alone. To do more than three levels it could be extended.
So the final solution to my problem, and unfortunately I can't say I fully understand it, can be found here on Andrei Petre's (old) blog. http://andreipetre.tumblr.com/post/26203496689/nested-formsets-with-django

Do I need to override save method to add data to different models from django UserCreationForm

I want to add another field in UserCreationForm to be shown in RegistrationForm , I saw a couple of examples on stackoverflow for that purpose. I mean the examples by defining different RegisterForm inherited from UserCreationForm as explained in this question of stackoverflow:
django-create-custom-usercreationform-basic
But what will I do, if my data belongs to 2 or 3 different models including User model? Will I then override save method or do some other thing? Is there some way to handle it without going to more low level by just handling it in RegistrationForm that will be inherited from UserCreationForm? What is better way?
Okay, like you see in the link, there are many approaches which you can use, no one of them looks such highlevel as you want.
I don't know how familiar you are with Django, but the linked appraoch looks very promising. It's fresh, uses the signals framework (flexible) and is very easy to implement - high level enough for your problem. Look out for UserProfile Examples because they are very similiar to your Problem and more usual.
If you don't want to go the way with signals, the most straight-forward solution would be to override the save method.
So you already had the solutions in mind. Imho I can't figure out a better or more high level solution.

Django project design question

I have just started to work on a django project( and learn django at the same time) and I came across some design questions that I can't really answer with my limited knowledge so I decided to ask it here. anyway Here is the questions:
1) where would you put raw queries. is it ok to put row queries in view.py files? My personal opinion is to put them only in models.py files.
2) where can you query db? can you call query methods in models.py, views.py, templates? I think they should be in models.py or views.py but not in templates. specifically calls like "MyModel__attribute_set__all" should not be used in templates.
Since I am new in django (and python) I am not really sure if I have the right idea about this. I appreciate for any feedback.
Sounds like you're on a good path already.
I try to:
Keep my views slim, in terms of code, and my models fat
keep my templates even slimmer and free of database lookups; if I have to do something that hits the DB that for some reason isn't viable to do in the view, I do it via a templatetag or filter so that it can improved and/or cached, and is also easy to find, and is as DRY as a can be
define and execute any raw SQL in the models that use it
where would you put raw queries. is it ok to put row queries in view.py files?
Queries are most commonly seen in the view.py; yes it's ok there.
My personal opinion is to put them only in models.py files.
If you're using the same query a lot then create a "manager" for the model. You'll put the very commonly used querys there. "Only" in there would be making life hard for yourself.
where can you query db?
Usually in views.py; not uncommonly in models.py.
can you call query methods in ... templates?
Technically it is possible but, logically, very strongly discouraged.
I think they should be in models.py or views.py but not in templates
I agree.

Django and Generic Views

I've written an entire app pretty successfully in Django but I have this nagging question that I think I know the answer to but I just want to make sure.
One of the things I really liked about Django was the data model and the ability to not have to do "obvious" stuff. For example, we use the admin interface extensively in our app. The fact that I don't need to write an edit screen for every model and keep it up to date every time the model changes is really nice.
What I'm puzzled by is that I wanted to have one part of the app render "read-only" versions of the models. Essentially I want exactly what I have in the Admin interface but without editable widgets. Now I notice, from the Django code, that that admin interface actually goes through and substitutes the widgets to use the editable ones so I know that non-editable is certainly there.
But as far I can tell, there is no way to just say "render this object" and have Django do the "obvious" thing and render it just like it does for the admin interface but with non-editable fields. I find this hard to believe since it seems like a) this is easier than the admin stuff and b) I know the widgets are already there. But I've looked all over and even the Django examples seem to always create a template and spell out exactly what the page should look like.
Writing a template is probably a good idea in general but early on in development when things are changing it would be better to have something that just does something basic given the information available in the model.
Am I missing something? Sorry if this is a stupid question.
Could be that most non-toy sites want a custom layout/html anyway?
Or, are you looking for Databrowse?
I used something like this: http://www.djangosnippets.org/snippets/937/
There are other similar things around if you google for 'django read-only admin' or similar.
Never underestimate how flexible the Django Admin is...