Is it possible to have seperate textboxes for request fields in Swagger UI instead of a single Body textarea for the whole model?
You should add the [FromUri] attribute to the parameter. That might, however, result in something you do not like or want as presented in this question but that can be circumvented by the answer I gave.
Related
I'm new to Django 2.0. I'm working on a project where I need users to input questions (from at least 1 to as much as they want). Like after adding one question they should have option if they want to add more in the same form field (like if I have a Textarea field it should be accepting multiple Textarea fields and can be displayed separately).
How can I do this, please let me know, thank you.
For your backend model, you have two options:
Have a single textarea field where users can type multiple questions. This is the simplest method for both frontend and backend, but will not be as clean.
Have a dynamic number of text area fields.
To accomplish the second method, you would have two models:
One representing the list of questions. Lets call this QuestionList This would most likely have a ForeignKey field pointing to the User
Another representing a question. This would have a textarea field, as well as a ForeignKey to a QuestionList. You might also want to have an IntegerField representing the order in the list.
Note: Also instead of textarea fields, you could just use CharFields, but you will have to set a limit to the number of characters.
I'm new in django and python. I want to have a form in one of my website's page. When user fills the data, i want to create an xml file with user input. so i need to access the user's data (form data) from views.py. I want to know am i allowed to only have form in html? if yes, how can i use it's data in views.py? i mean should i have always a class defined in forms.py that contains my form fields? If the second way is a rule, what should i do with templates(html)? (It means if i define my fields in a class in forms.py, should i redefine them in templates(html) too? and how can i use data in views.py)
i need some kinds of input in my form: checkbox, input type:text and input type:file
I really need your answers. Thank you :)
You can totally define your form in raw html and write a view which just processes the post data. But then we come to point where you would invent the wheel once again. Django provides a bunch of field classes which provide html rendering and data validation (never trust user input).
I suggest you read the online documentation for forms.
I guess this is a simple question, but I am not being able to find a definitive answer whether this is possible or not.
I have a Model object that is being passed to my template during rendering. While I use most of the Model fields to show their values, I came across a need to show a few of them as formfields (with their respective HTML element like rendered via ModelForms). Can ModelForm class usage be avoided here and simply use the Model's field to be rendered as formfield directly from template?
an example:
class MyModel(models.Model):
month = models.PositiveIntegerField(_('Month'), choices=MONTH_CHOICES)
year = models.PositiveIntegerField(_('Year'))
Now in template based on the above Model example I want to show the year as a value but month as a dropdown with selected current value?
Any pointer appreciated!
Use case:
Imagine a generic list_view like case where you are rendering a queryset. What you have there is object per iteration, however on the list view you want to allow your users to perform some quick editing of few object attributes to avoid forcing them to go to full edit mode (maybe via simple ajax).
No.
As canvassed in the comments, this is not possible. A model, and its fields do not know about forms - that is what forms, and modelforms are for.
The right way to do this is to wrap your model objects with a modelform, which you can adjust (using inheritance) to do whatever you want with the non-form fields, if you wish to make this more transparent.
Creating modelforms in the template means that you have nowhere to process the forms in django. Apparently you want to interface them with some kind of ajax interface, so perhaps this is not a problem for you, but in any case, I fail to see how it would be easier than simply creating the modelforms in the view.
Before I start on this project I want make sure what I am proposing is feasible. There is an unusual requirements I have to satisfy.
The model and formModel fields have to support a comment field on every field. I would like to be able to access them by object.field and object.field.comment. At first glance, this looks like I should have a separate type of comment and then somehow link it to correct field but is there a way to create a custom modelField that stores data in more then one database field from one or more form inputs? I would think there would have to be a custom widget to support this too?
If anyone has any ideas or examples of anything like this please respond.
I think this is the answer to my question. I have not tried it yet, but from the description it looks like what I need. https://docs.djangoproject.com/en/dev/ref/forms/widgets/#multiwidget
I have a complex object that I'm storing serialized in a text field. For most purposes, I want the object pulled from the database to be that complex object. However, when I'm editing it in a form, I just want to see that serialized text in the field.
I tried using the value_to_string function but it appears as if it isn't being called at all when editing the record from with admin.
What do I do so that the raw serialized text shows up in the admin text field?
Since the admin already uses the value of your model field, one option would be re-serializing it for editing...
See also formfield_overrides:
This provides a quick-and-dirty way to override some of the Field
options for use in the admin. formfield_overrides is a dictionary
mapping a field class to a dict of arguments to pass to the field at
construction time.
I'm not sure if it's the same problem, but I figured out a way to change the field value in the admin form before displaying it. I explained how to use a custom widget and an custom admin form in this answer.
Note that the custom widget only helps you to display the value in a different format. It won't parse the input value back to an object, although I believe that would be possible too.