Fetch external data and populate form in django admin - django

In short:
I would like to fill in some form fields in django admin, before I click the save button, based on the input in another field.
The longer version:
I have 2 models, SteamGame and GameInfo which has a ForeignKey to SteamGame. The SteamGame table gets filled once a day with all the games listed on Steam, by fetching and parsing http://api.steampowered.com/ISteamApps/GetAppList/v0002/, the GameInfo table gets filled manually using the admin site. When adding to GameInfo I select the game from SteamGame. Some of the fields are for info I add myself, some of the fields I want to be populated after selecting the game and fetching https://store.steampowered.com/api/appdetails?appids=GAMEID&l=en for that information.
Are there some magic Django functions I can use to accomplish this? Or any other suggestions on how to do this?
Thanks!

you can pass a javascript file and using query you can make a ajax call and append the values to the form using jquery .
class HistoryConfigurationAdmin(admin.ModelAdmin):
class Media:
js = ("customjs/yourjsfile.js",)
and create a yourjsfile.js in static folder of you project.note for using jquery in django admin instead of using $(document).ready we use "jq" instead of "$" symbol

You can use javascript to achieve that.
On your admin model, add this to call for a custom javascript:
class MyAdmin(admin.ModelAdmin):
class Media:
js = ('/static/js/myMagicJavascriptThatDoesAllThisStuff.js',
)
The JS will allow the user to see what will be saved BEFORE saving it.
If you want to pull the information after the save, you should override the admin save function

Related

Overriding django admin pagination along with dynamic values

I am working on a project, I have a requirement from my client he wants to implement a dynamic paginator in django admin panel. Requirement is when user input 10, ten record will display per page same for 20,30
is there any way to do it.
See ModelAdmin.list_per_page. The default is 100 but you can change it to whatever you like.
class UserAdmin(admin.ModelAdmin):
model = User
list_per_page = 5 # No of records per page
admin.site.register(UserAdmin)
If you want to dynamically change it I'd assume you'd use an ajax with a GET for the page number otherwise you'd have to do a lot more and alter the default admin template used by django.
Check out this approach if it helps you .

Where is ID saved in django ModelAdmin autocomplete_fields?

I am rewriting some administration interface to django 2.2, currently using django autocomplete_fields admin feature. Simply said I have ModelAdmin object OrderAdmin, which has nested TabularInline ProductAdmin: variable-length table of products which might be added to order. Each of these ProductAdmin holders just contains ForeignKey to actual product class, with some other attributes.
Now I wonder: where does django store id - ForeignKey - of item selected with autocomplete field? It doesn't mark OPTION in selectbox as selected, and although there is suspicious hidden input field with #cashregisterproduct_set-0-id on page, it doesn't have any value. Or is there some special way how to access it? I was thinking about adding id to __str__ method of model and parsing, but thats just ugly.
Thanks for tip.
EDIT: to make it 100% clear, where from does django get ForeignKey of object selected through autoselect_field, when creating new object from ModelAdmin?
I got misguided thinking that this is managed by django. Selected data might be accessed by using select2 framework:
selected_value = $('.myselectbox').select2().val();
related: https://stackoverflow.com/a/47451658/16268461

add user friendly json editor to django admin

I have a django app, using also rest_framework, and a model Product with field of type JSONField. so data is stored as JSON in Postgres, Now I want to provide the admin with a nice user friendly way on how he can change the json field (names/keys and values). is there an extension for that or is there a faster way on how to do that.
here is the column definition in the database.
my_column = JSONField(default={"editorial1": "text 1", "editorial_2": "text2", "editorial_3": "text"})
BOTH KEYS AND VALUES SHOULD BE EDITABLE BY THE ADMIN
The admin should not know anything about JSON, and should not enter/edit any json format field
You can use prettyjson's PrettyJSONWidget:
class ProductModelForm(forms.ModelForm):
class Meta:
fields = (
...
'my_column',
)
widgets = {
'my_column': PrettyJSONWidget(),
}
I ended up using the django-admin-json-editor. Not the best thing in the world, but it does the trick
https://github.com/abogushov/django-admin-json-editor
You can try https://github.com/jrief/django-entangled
In comparison to the editors mentioned above, it doesn't replace the widget used to render the JSON, but allows to override the ModelForm which otherwise is generated by Django's ModelAdmin.

Django Admin Page: Help Text for Model Methods?

I have a model method in Django that I am displaying on an admin page just like I would a model field. With a field, I can just add a help_text argument to it to give a description of what the field is and what the user should put into it. However, with a model method, help_text does not work. Adding the attribute short_description changes the way the method name is displayed, which is sort of okay, but I'm looking for a way to add a few sentences of description beneath the method value that is displayed. Is there any way to do this natively, or would I have to resort to overriding admin templates or something? (Which I do not think is worth it for something this minor).
You can do this using JS.
Replace ID-OF-THE-FIELD with the actual id of the desired field.
(function($) {
var myField = $('#ID-OF-THE-FIELD');
// find the id of the desired field by doing
// Right-Click > Inspect element
var help = $('<p class="help">A very long help text</p>');
help.insertAfter(myField);
})(django.jQuery);
Put this code into a JS file and supply this file using class Media of your ModelAdmin class.

Insert django form into template dynamically using javascript?

I want to add same django form instance on same template. i already add one before and other add dynamically using javascript.
for example 'form" is a django form: newcell.innerHTML = {{ form.firstname }};
The problem is that when i submit the form, in view the request object has only one value (that is not add using javascript). how can i get the values of other form elements values that is added dynamically runtime.
It is something like the "Attach Another File" feature in gmail, where the user is presented with a file upload field and new fields are added to the DOM on the fly as the user clicks to "Attach Another File" plus button
You could always try separating out your FileField into a FileModel.
Take a look at the following pseudo code (as in python based on memory--i've moved over to clojure for now).
models.py
class FileModel(models.Model):
file = models.FileField()
...
class ThingToWhichYoureAttaching(models.Model):
name = models.CharField()
attachments = models.ManyToManyField(FileModel)
...
forms.py
class FileForm(forms.ModelForm):
class Meta:
model=FileModel
class ThingForm(forms.ModelForm):
attachments = forms.MultipleChoiceField()#override the manytomany form field with style field of your choice.
class Meta:
model=ThingToWhichYoureAttaching
When they pop up the window with the PLUS button, show the FileForm but on the main page leave the ThingForm untouched. You can also have an initial FileField on the main page with the ThingForm for people who don't have javascript. Just make sure to process the FileForm BEFORE the ThingForm so that the File is available for the Thing.
When processing the popup form you can use AJAX (i recommend jquery) to submit the FileForm to the server, and return the markup to insert the File in the Attachments field.