I want to add, some fields depending on others. I have city and country model. I can include country as foreign key in city model. And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax? If not then what is correct way? and also is there a way to add city on the spot while adding main content data if city is not already on list?
So are above possible by using django admin or is it that django don't give? If not then how can it be done in django autogenerated admin?
You can do exactly what you ask using django-smart-selects
Hope that helps...
I can include country as foreign key in city model
This seems like a good idea.
And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax?
No, it will not get filtered automatically, you will need to write that code yourself. Both in admin and on frontend.
and also is there a way to add city on the spot while adding main content data if city is not already on list?
You will get this in the admin area.
Go ahead and start doing it, and when you run into specific problems, post them here if you can't solve it. Also read the Django docs, it is pretty elaborate on the topic of models.
Related
I have a model:
class Rent(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
bikes = models.ManyToManyField(Bike)
when I create a new rent in the admin site, I would like to exclude the bikes that have the value of 1 for their status integer field from showing in the list to choose from. Is this possible?
Thanks
I am not sure if you can do that from the Admin site. probably you cannot. But you can do it from views. just select the model objects you need with query and pass it into the field. I am attaching a ell explained similar scenario. condition your query and add those objects.
Adding many to many fields based on condition
You can customize the admin form. Here is a discussion of how to filter your form choices.
Thank you all! I figured it out, the problem was that the limit_choices_to doesn't work when I change the filter to inline and I found others with the same problem, so I just changed it to filter_horizontal and now it works as it should
I have the following model:
class Owner(models.Model)
country = models.CharField(max_length=255, choices=COUNTRIES, default=COUNTRIES_DEFAULT)
COUNTRIES is compose of tuples:
COUNTRIES = (
('afg', 'Afghanistan'),
('ala', 'Aland Islands'),
('alb', 'Albania'),
('dza', 'Algeria'),
('asm', 'American Samoa'),
....... )
For FrontEnd, I need to show a Widget, and for each country to have a checkbox.
A Person/User can select multiple Countries.
I presume I need to use a custom widget, but I don't know where to start inherit for Field/Widget and make the query in the database.
--- Why is not a duplicate of Django Multiple Field question ----
I don't need a new Model field, or to store it in the database and is not a many to many relation, so something like the package django-multiselectfield is not useful.
The ModelField is storing just one value, but in the form will appear values from the tuple.I added just to see the correspondence.
Instead I need to be just a Form Field, to get the values, and query the database. Like get all owners that resides in USA and UK.
Also is not looking like Select2, I need to respect design. As functionality is like in the image:
In your Form you must define a MultipleChoiceField with the CheckboxSelectMultiple widget:
countries = forms.MultipleChoiceField(choices=COUNTRIES, widget=forms.CheckboxSelectMultiple)
This will give you a list of multiple choice checkboxes. You can style that yourself to appear with a scrollbar if you don't want to show a long list.
Here is an example from the Django documentation: https://docs.djangoproject.com/en/2.1/ref/forms/widgets/#setting-arguments-for-widgets
I want to model a schema for an award function in django. I have a User model, a show model and an award model. A user can get one award for a show. I can simply say User 'aaa' gets award 'bbb'. but then that award cannot be associated with other user for another show.
I think there is three way modeling required for User, show and Award. Is there any sophisticated way of doing it? I know about 'through' but dont think this can be a good tool here. If it is could you please guide me how to?
You can have an extra model, I will call it UserAward.
class UserAward(models.Model):
user = models.ForeignKey(User)
award = models.ForeignKey(Award)
show = models.ForeignKey(Show)
When I try to enforce two unique constraints only later one stays.
Part of the model of an app I'm trying to build with django is based on a three-tier structure, like this :
class A(models.Model):
name = models.TextField()
class B(models.Model):
related_A = models.ForeignKey(A)
info = models.TextField()
class C(models.Model):
related_B = models.ForeignKey(B)
other_infos = models.TextField()
So A is composed of some Bs, which are composed of some Cs. Since Django admin doesn't accept nested inline forms yet, I was thinking the ideal way of doing it would be with this sort of workflow : there would be a "A admin page", which would get me a link to click called "Add some Bs to this A", and offer me a page with a B form using inline C forms.
Is there a way to do this simply from the Admin website that I didn't see, or should I consider extending the admin default application ?
Some clarification
Let's say you make statistics about Continent, Countries and Regions. My goal is to have a clear structure to add a Country to a Continent and a Region to a Country. Ideally, a user of the administration interface won't be able to add "immediately" a Country ; she or he needs to go through a page where he'll have to select a Continent. Ideally, there would be one single page for the Continent, containing an inline form of country, each of those forms having an inline form of regions.
Since nested inline forms do not seem to be a feature in Django 1.3, I'm looking for a way to have a sort of "workflow" in the administration site : the user would first click on "Continent" in the first screen ; it would then show a list of continents ; clicking on a continent would show a list of the countries associated to this continent ; finally, the user could edit a Country form, displaying some inline region forms to allow adding country and region on the same page.
I hope this concrete exemple make it easier to understand...
U can use GenericForeginKey() to solve the limitation problem.
Ref https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
The easiest way is to use Country as a start point: in its form page you could inline edit Region as well as force staff to choose from existed Continents or add new one by clicking +.
You could also modify ModelAdmin to open Country page and populate pk of a saved Continent to it, which is more difficult. Check this post also.
Furthermore, it's possible to mimic a ForeignKey pointing to A in C, and inline editing both B and C in A, just as GenericForeignKey does. However, it's hard and overkilled for most usages...
Two questions please:
I need two foreign keys back to User, one for author and one for coauthor. I have managed by setting related_name equal to + but am not sure if this is smart or the best way. Thoughts or pointers?
When making an add entry via the django admin for this model, the author choices are names like john_smith, etc. Where would I call get_full_names() from in order to display full names rather than usernames with underscores? Thanks.
from django.contrib.auth.models import User
from django.db import models
class Books(models.Model):
title = models.CharField()
author = models.ForeignKey(User)
coauthor = models.ForeignKey(User, related_name='+')
Kevin
I would change the related name to a value that is more intelligible - such as books_where_coauthor and also add a similar one of books_where_author as then you can get the relevant books by going from theuser.books_where_author.all() etc
Regarding your Admin query, you're getting the username because that's what the default __unicode__() method of User spits out.
Unless you'd like to hack your contrib.auth.models file (not recommended), I'd suggest using a custom modelform in the admin, and manually setting the names of the choices in the ModelChoiceField, either by subclassing that field and making a custom one that renders its widget with get_full_name if possible, or do it via something like this snippet. That said, I am sure there's a simpler way to do that, but I've forgotten. Dangit.
With regard to the second part of my question (displaying full names instead of usernames in the ChoiceField of a form), I found this link to be just the ticket: