Autocomplete a specific set of possibilities in Django-admin - python

I have a field in a Django model, and I want there to be a small (~20) set of possibilities (which are strings) which can be autocompleted (preferably with django-autocomplete-light, which I am using already) in django-admin. Should I make this a foreign key field and create a model containing just these 20 possibilities? or is there a better way?

django orm limit should be done something like:
Entry.objects.all()[:5]
so creating a new model for limiting number of results would not make sense.
I've looked at django-autocomplete-light and saw limit_choices here (didn't give it a try.
in any case it should be something like line 77 in this snippet.
I hope this is helpful.

The only way to do this is to have a separate model with only those possibilities and to make the field we want to limit into a foreign key field.

Related

Customizing model fields by user

I am making a CRM, and I ran into one task: I want to make a “Client” model, with all possible fields, and give an opportunity for users to “enable” only those “Client” fields that he needs.
I have little experience and unfortunately I have not been able to find a solution for this for a long time.
I would be grateful if someone can show me an example of how this is done (or a link to a repository with a similar method).
The answer is JSONField, new DBMSs support JSON Fields natively and Django has a native support from 3.0, so you can add the extra fields as attributes in a JSON and save to the a column called extra for example you want to add a field called 'mobile2' so rather than creating the column, so you can add it to the extra column like this
obj.extra["mobile2"] = "0xxxxx"
this allows you to extend quickly and give different attributes per user.

Show change log history for model instance using django-reversion

Using:
django 1.10 reversion 2.0.8.
My question is how to show a nice list of changes done to a given model instance. By that I mean that the user can quickly see a list of all the changes (new values for fields) in all revisions. He doesn't need o see all the fields only the new values of the changed ones.
So I found that a good tool for storing changes is django-reversion. However, I cannot find a solution for my problem which as I mentioned is to show a nice change-log history for a given model instance.
I found solution that can compare two revisions django-reversion-compare, but that is not what I am looking for. Maybe there is a better tool for that ?
The task is too quickly show to user what was changed by who and when. The model is simple and doesn't store a lot of data. It does store however foreign keys.
I was also looking to do the same, and after reading up a few SO posts, docs etc., it seems I had to roughly choose the solution from one of the following 3 approaches:
1) Fetch the existing model instance before saving the new model instance. Compare each field. Put the changed field in reversion.set_comment('(all changes here)'). Continue with saving the model instance.
2) Save a copy of the old fields separately in model's __init__() and later compare the new fields with them (in model's save()) to track what changed. Put the changed fields in reversion.set_comment('(all changes here)'). Continue with saving the model instance. (This approach will save a DB lookup)
3) Generate a diff using django-reversion's low-level API and integrate with the Admin somehow
I ended up using django-reversion-compare which worked great for me showing the edits wiki-style (which may be using (3) above anyways)
django-reversion's developer also confirmed (3) as a better option which also avoids race condition.
If you would like to explore different options, this is a great SO post with lots of good ideas with their pros/cons.
(I am also on Django 1.10)

Django searchable drop down for ForeignKey widget

in my Django model I have a m2m relation with an intermediate model that contain the two foreign keys of the respective two models.
My problems is that, the first model may have many entries and when I associate it with other model I have a too large drop down widget in the admin.
The default ManyToManyField widget (with filter_horizontal set) may be ok, but it is compatible only with m2m relations....
Is there a solution (even 3rd parts) for the ForeignKeyFields?
Any suggestions?
Edit:
For the other users, I've solved my problem using django-selectable
http://django-selectable.readthedocs.org/en/v0.8.X/index.html
The 3rd party component Django Ajax Selects does exactly what you are looking for.
I played around with django-selectable as well, but found django-autocomplete-light (https://github.com/yourlabs/django-autocomplete-light‎) easier to use and more flexible for what I wanted to achieve.
Perhaps it can be useful to you as well.

Django inline admin problems

I'm trying to do such a thing: I have two models, connected via Foreign Key, and every instance of first model needs to be linked with exactly 4 instances of second (I use InlineAdmin to create them at the same time using extra=4 and max_num=4).
There are two problems:
1 - I need each of four models to has it's own default read only value for one of it's fields, this value needs to be selected from the field's choices option. But I need them to be read only (I know about readonly_fields, but it's useless for that. Javascript don't seems to be a good solution...
2 - I need to specify default values for some field for all four models at the same time editing only one field. I'm thinking of two possible solutions: javascript or one additional, "fifth" model with all hidden fields except the one I need so I can override save() to use it's values for other models and delete it.
But which is the right way?
Try this documentation, it might help:
https://docs.djangoproject.com/en/1.5/ref/models/fields/#editable

Display custom labels for values in the Django Admin Site

One of my models has a 'status' field which is only ever modified in code. It is an integer from 1 to 6 (although this may change in the future).
However, in the Admin site, I would like to display a label for this data. So, instead of displaying '5', I would like it to say 'Error'. This means I would be able to easily filter the objects in the database that have the status 'Error' and also my colleagues who don't know what each status means as they are not involved in coding, can use the admin site to its full.
I don't know if I am going the right way about this or if it is even possible, but I would appreciate any help you can give. I would rather not change how the status is stored as it would require a big re-write of some parts of our system. In hindsight I guess it was a bad way to do it, but I didn't think the field would matter as much as it does.
Consider to use choices. Anyway you can customize lots of things in django-admin, just read the docs:
http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
You could create a custom field type that overrides to_python and get_prep_value to store integers in the db but use string values within Python:
http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.django.db.models.Field

Categories

Resources