I'm trying to make something like the image below. Let's say that I have 3 multiple choice fields like the following. If I choose bags from choices_first, I want choices_for_bags to show up as the next choice step.
The below image is one of the fields for Django default user model. It's for another purpose. I'm looking for something like that. As I choose something on the left, I want something to show up on the right based on the choice on the left. Does Django admin provide anything like that as default or is there any library for that?
choices_first = (
('clothing', 'Clothing'),
('bags', 'Bags'),
)
choices_for_clothing = (
('tops', 'Tops'),
('bottoms', 'Bottoms'),
)
choices_for_bags = (
('handbags', 'Handbags'),
('backpacks', 'Backpacks'),
)
As far as I know django-smart-selects provide ChainedManyToManyField option which should be similar to User Permissions scenario since its ManyToMany field, but I'm not sure if it will work as you need.
Related
I want users to choice 2 selection of my checkbox how can i do that by using Django?
I ve tried that
forms.py
class KaydolForm(forms.ModelForm):
secim = (('Bk','banka kartı'),('kk','Kredi Kartı'),('cek', 'çek'))
secims=forms.ChoiceField(choices=secim, widget=forms.RadioSelect)
but i can choose only one of them
https://docs.djangoproject.com/en/3.1/ref/forms/fields/#multiplechoicefield
Read this, in your form it would be like this:
class KaydolForm(forms.ModelForm):
choices = (
('Bk', 'banka kart1),
('kk', 'Kredi'),
('cek', 'cek')
)
secims = forms.MultipleChoiceField(choices=choices)
You should use a MultipleChoiceField. On this field you can select multiple options.
Then you should also use a widget (representation on webpage) that accepts multiple choices. You could use CheckboxSelectMultiple or SelectMultiple
So change your code to:
class KaydolForm(forms.ModelForm):
choices_list = (('Bk','banka kartı'),('kk','Kredi Kartı'),('cek', 'çek'))
choice_field = forms.MultipleChoiceField(choices=choices_list, widget=forms.SelectMultiple)
The user could now however check one, two or three options. You must write a own validator to check, if exactly two are checked. For more detail see here
I'm using MultiSelectField in my form, which allows the user to select multiple choices:
models.py
physical_limits = MultiSelectField('Physical Limitations', choices=PHYSICAL_LIMIT, max_length=255)
screenshot link
However, I would like to add few additional fields per choice, e.g. min value, max value, units, etc (for the user to fill in, for each choice he makes).
For example, if the user selects option (2), which is "Solid", he will then be able to fill in additional corresponding "sub-fields" for this selection.
In some way, I feel like I'm looking for a different field type, which acts more like a table or array, for the user to fill. I couldn't find such a thing though..
"Vision"
Can you please guide me how to achieve that goal?
Thanks ahead,
Shahar
I am trying to write a Django app that does the following:
A user sees various articles and then codes variables against these articles.
i.e.:
Article is about Egypt.
User assigns: country = Egypt
This, so far, is easy.
What I would love to have, though, is that the user can create the variables himself, without me having to hard-code them into models.
How do I best do this?
Should I use the through-relationship on a manytomany-field or are there other, better, ways to do this?
If I use the through-relationship, how can I let the user choose what data-type the variable should be?
Should I put a field for every fieldtype into the through-model and then have the user choose it somehow?
I know, this is more than one question, but if you answer my first question I would be very happy!
I am assuming that by "user" you mean other apps that are based on the app that contains Article.
Use model inheritance:
https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance
class Article(models.Model):
pub_date = models.DateTimeField(...)
class CountryArticle(Article):
country = SomeSuitableModel()
I am writing a django application, that stores and displays working hours of employees.
The problem is, that for example pediatrists have 2 types of working hours - separate for sick children and healthy ones.
So I thought, it would be cool, to use HTML table to display the hours for each employee. My idea was to have a "ListField" representing each row of table, with ForeignKey to employee. That way, admin could create lists like:
['', 'Sick Children', 'Healthy Children'],
['Monday', '8-12', '12-14'],
['Friday', '12-15']
And it would appear on website, as an HTML table, that would look pretty nice.
The thing is, I would love it to look easy and intuitive for an admin of website. So I would love to keep the table rows as inline of employee in admin panel.
So, I have created models:
class TableRow(models.Model):
employee = models.ForeignKey(Employee)
class TableCell(models.Model):
content = models.CharField(max_length=20)
row = models.ForeignKey(TableRow)
And tried stuff like:
class TableCellInline(admin.TabularInline):
model = TableCell
class TableRowInline(admin.TabularInline):
model = TableRow
class EmployeeAdmin(admin.ModelAdmin):
inlines = [TableRowInline]
admin.site.register(Employee, EmployeeAdmin)
admin.site.register(TableRow, TableRowAdmin)
Which doesn't work (as I expected, but didn't hurt to try). Admin panel shows an option to add table row, when adding/editing employee, but doesn't show any option to add any cell to the row.
Is there any way to allow adding the rows while editing/adding employee? Or maybe a totally different way to solve the problem?
What you're trying to do is commonly referred to as nested inlines. Unfortunately, I'm afraid this is still not supported by the admin. See the following resources for more information.
Nested inlines in the Django admin?
#9025 assigned New feature: Nested Inline Support in Admin
I'd like to know what is the best way to store users' preferences in Django.
Let's say I have three choices users can select.
Based on their selection I want to customize certain templates.
Is it better to save each choice/preference as BooleanField() or as a tuple of choices?
Boolean:
subscription_newsletter = model.BooleanField()
subscription_posts = model.BooleanField()
subscription_promotions = model.BooleanField()
Tuple:
SUBSCRIPTION_CHOICES = (
("newsletter","Newsletter"),
("posts", "Posts"),
("promotions", "Promotions"),
)
It depends, whether you want them to be able to choose just one option (then tuples) or many (then booleans).