Django inline admin problems - python

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

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)

Autocomplete a specific set of possibilities in Django-admin

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.

Django - how to validate custom form for adding many similar entries at once

I have a model called "occurrence" which tracks occurrences of species at different sites. The model has 4 fields.
refID (foreign key to the reference source of data)
siteID (foreign key to site)
speciesID (foreign key to species)
abundance (integer)
I know I could create a model-form to add an entry. But modelforms would be tedious because, most of the time I want to enter data for dozens or hundreds of species with the same combination of siteID and refID. I have created my own data entry form in the template to select a refID and siteID, and use jQuery to add new lines for speciesID and abundance. Thus, I have a single refID + siteID combination, with many speciesIDs + abundance lines. Then, the idea is to iterate over all the added lines and save all the occurrences in the view.
The problem is that validation of this form is quite difficult, as I have to do everything "manually" in the view. This seems like it might be a common problem, so I wonder.....
Am I missing a pre-existing Django solution here?
Probably you're, as far as I understand your question.
have a look at Formsets
The simple solution to this problem turned out to be using a modelform to save a single instance at a time. I made the refID and siteID fields "sticky" by passing back the saved values for those fields to the reloaded form using the "initial" argument to the modelform. This way, I can use all the built in form validation.

onetomany relation field in Openerp

I try to create a related field on OpenERP 6.0.1 . is it possible to define two different onetomany relationfor the same field name? What all changes i must do in the(.py file and XML Files).
No you cannot do that:
the field names are keys in a Python dictionary, in what you write the second invoice_line will overwrite the first one
this would mess up OpenERP's ORM anyway as it does not handle relations to different tables.
So you need two different columns, one relative to account.invoice.line and the other to account.service.line. If you really need a merged view, then you can add a function field which will return the union of the invoice and service lines found by the two previous fields. But I'm not sure the forms will be able to handle this.

Categories

Resources