Like a many other users here i have been thwarted in trying to get nested inline forms working in the django admin.
I have a data structure that is roughly
Group
- Some data fields
Patrol
- Foreign key to groups
- Some different data fields
Scout
- Foreign key to patrol
- Even more data fields
Ideally when i go to edit a Group record, i would have the admin interface present the Patrol instances linked to scout shown though StackedInline and then have the Scout records related to each of these patrols show up inside an inner TabularInline however this is not currently possible, i have spent a large amount of time trying to port over a patch from two years ago but have had little success.
As far as i see it i have three options open to me
Persist with trying to get this patch from an age ago to work
List patrol objects within a TabularInline without the scout relation and provide an edit patrol members link that pops up a form to edit a patrol in a new window.
Just keep it simple and make users of the system go back and forth between editing group and patrols
Option 2 is what currently looks the most promising but i need advice as to how i could pop up an extra window to edit the patrol in.
Any advice on how to do this, how to get nested inline fields to work or another way in general would be much appreciated.
Related
I have a model with many country fields and each is a choice field wit list of countries.
The issue is that there's a lot of countries! So each choice field adds a lot of size and rendering time to the page to generate a select dropdown in django admin. (E.g. Making 5 of these read only brought down response time from 10s to 4s!)
I want to see if there's any alternatives known for handling choice field inputs. Since we use ISO-2 for countries, I want to avoid operators having to input values directly as they would not know them.
Ideally I was hoping for some sort of search or select pop-up similar to what happens with raw_id_fields. That way a select list is only generated when needed, but so far havent found
You may try django-jet package or one of its forks. It is fully rewritten django-admin template, and it supports typing search for model choice fields. Or try looking for another package in django packages
I recently forked Saleor 2.9 for a web app I am building for an art gallery that wants to display their products for sale as well as give their artists some publicity. I want to be able to have a bunch of cards (like "our team" components) that pull data from an Artists table on the back-end that stores information about the artists' names, emails, origins, etc, and then display it on the front-end. I am struggling to see how to modify the models/DB to create a new "Artists" table with name, email, info, and then to create a manyToMany-like relationship with the products I've populated in the DC, giving the products a "created by" attribute. There are tons of models files throughout the /dashboard directory, and even when I make changes to the core models to create an artist class, I don't know how to get it to show on the dashboard so artists can be created/modified from there.
I would like to make it so that the client (non-technical) can add artists and have them show up on the artists page I will make, somewhat like products show up on their pages (but obviously I cannot create a new category "Artist" as artists cannot have prices or shipping as they are people; and there are other attributes I would want like email that a product cannot have, either. They are also different to staff on the website, so I cannot use the "staff management" functionality.)
I looked at this question but Saleor structure has changed since then, and that was a relatively minor attributal change to an existing class (User) as opposed to the creation and integration of a new class. I'm surprised that despite extensively searching for anything on how to do something as straightforward as create a new model there is little documentation and discussion online; I must be missing something.
Please help :) Thank you!
The django way to create new models, (and SaleorĀ“s backend is django based) is:
You should create a new app on your store backend (the django part of saleor) with:
$ python manage.py startapp artist
Create your Artist model, with all the fields you want such as email, etc... in the file: artist/models.py.
Modify the Product model in the file product/models.py by importing the Artist models and adding a ForeignKey (for example) relationship to it.
Register the new artist app in your settings.py's "INSTALLED_APPS".
Run python manage.py makemigrations... (Check they include your changes to models)
Run python manage.py migrate.
That should be it. 'less I`m forgeting something, in which case, please post back when you have moved forward with this.
Notes:
You may want to back up your DB first.
Also when applying these migrations, django will ask you for a placeholder value for products which where in your DB before Product had an Artist field.
References:
django models
django migrations
I'm working with Flask-restplus and I am at a point where I would like to associate each User in my user model to a type of profile, where each user can be associated with one or many profile types. I'm wondering how you guys would go about this. So far, here's what I'm thinking/planning to do. NOTE: I'm not very experienced in web development, so there's a chance I don't know the best way to accomplish this.
Step 1: Create a one-to-many (clients need to also be employees, see below) field (profile_types) relating to a static table that just lists all possible profile options. EXAMPLE:
PK PROFILE TYPE
1 provider
2 employee
3 client
.....
The idea here is to track different information and allow for different views/endpoints for users who are tied to certain profile types. Example, employees would provide a valid login authentication and be directed to page A while a client would be directed to page B, etc. We're also planning on collecting different data points within each profile model.
So an instance of a user might look like this, user1.profile == [client, employee'].
NOTE: This is more important for data collection (ie age of employee, start date, etc) than it is for creating directives based on permissions.
Step 2: Upon creating the new user, a signal fires off the creation of a profile based on the profile_types supplied to the user instance. I've used signals with django in the past, is there a signal library for Flask?
Step 3: An empty profile instance(s) now exists for that user. It will be up to a superuser to upload data in bulk or manually fill out profile information.
Is this a sensible way to go about this? My other though is to create a bunch of Boolean fields in the User model is_provider, is_employee, is_client, etc. All fields that are True get assigned a profile instance. What's the best way to go about this?
Thanks everyone!
Seeing that are you try to validate multiple profile types, you may use
if user.profile_type in ['employee', 'client']
Now, if you want to add an super user I think you can use this
if user.profile_type in ['employee', 'client'] and user.profile_type == 'superuser'
Now, you relationship is more like 'many-to-many', because you are saying that an client also needs to be an employee, if you mean that some endpoints needs to be accessible for employees and clients, then you need to use a 'many-to-one' relationship (an Stackoverflow question which explains what is that)
For your instances, there is Flask Marshmallow, which has an amazing compatibility with Flask SQLAlchemy if you are using an database, but Flask Marshmallow can work alone.
I'm trying to get a field of model in Django, which will behave like a ChoiceField but:
will support multiselect
would allow to edit the list of choices
For example - I've got a product, which belongs to several cathegoris: nice, for boys, for men and so on. One product could belong to several categories. Categories can vary across the time.
Currently I'm using MultiSelectField from djang-multiselectfield, which - obviously - handles the first issue.
Is there a ready to use app, which also supports edition of choice list? Or what would be the easiest approach to achieve that?
I'd like to add a "user" column to the list_filters for a Django modeladmin. The model's user column only contains a dozen unique users, however, I have thousands of users, causing the select field to be rendering with thousands of options, making it unusable.
How would I make the select only show the users that are actually used by my model, or at least use some other widget for rendering the select, so the user doesn't have to scroll through thousands of options?
Your question is a little confusing. At first glance, you want to limit what items show up as available filters on the changelist Filters sidebar, but then you go on to talk about selects, which seems to imply you're talking about limiting the options for a field on your change form.
If the latter is the case, #kgr's answer is appropriate, however, if you're asking about the former, see my question and answer regarding a similar thing here on SO