I have a model Sessions that contains almost 10,000 objects. Each session has a foreign key relationship to a Subject. The problem is that when loading a page that has a foreign key field to a session, the page takes a long time to load.
To fix this, I like to have an admin on which you select a subject from one ForeignKey field, and have the sessions that have a relationship with that subject appear in a second ForeignKey field.
Is this something that is possible from within the django admin interface? I have checked out django-smart-selects (though I'm unsure if that would actually do what I'd like it to), but when I try to import it I get an error "No module named admin_static", which I believe may be because I'm using an outdated version of Django (v 1.1).
Any help would be appreciated. Thanks.
Are you sure you don't just need raw_id_fields?
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
When you visit an admin change form that has a foreign key to Sessions it will build a select dropdown with 10,000 entries. This is probably your slowdown.
Related
Novice alert! I want to use django-csv-importer 0.1.3.5 to import data to models with ManyToMany relationships.
Should make a different model for every relation or should I put everything in the same model?
Also, note that I want to allow registered users on my site to submit a form to that same model, would that still work?
I use django import export which is easy to use and supports many file types.
It comes with admin integration. I haven't tried many too many but tried with a foreign key, it works pretty well. Yes, the user can submit the form as well.
See https://django-import-export.readthedocs.io/en/latest
I have coworkers to upload posts through Django admin. The problem is they keep making duplicate posts as we've been covering a lot of posts. Is there a way to find out if a post already exists when typing a certain column or submitting?
I searched about that, but didn't get any useful information.
Your business case seems to be a text that is duplicate if it is case sensitive equal.
On a DB and Django Model level you assure unique entries by adding unique:
class MyModel(Model):
my_field = TextField(unique=True)
To check during input you need JavaScript in the client and an AJAX endpoint on the Django server side. It's actually an Autocomplete/Autosuggest functionality for that field. There are several packages that might help you with that. Out of the box, the Django Admin does not support this.
I am using django-salesforce and I would like to create a model within Django that has a ForeignKey field pointing to a SFDC model (hosted on force.com).
I created a custom model on force.com, let us call it SFModel, and I can successfully work on it from django (CRUD) by subclassing salesforce.models.Model.
I also created a django.db.models.Model, let us call it DJModel, that has a unique field ForeignKey(SFModel). This model is registered on the admin panel.
All models validate and I can go to my admin panel to try to create a new instance of DJModel. However, when I try to display the create_form in the admin I get the following error :
hasattr(): attribute name must be string
and the debug stream says
So I tried to set an arbitrary alias to the SF entry in the DATABASES of my settings.py. There is a dedicated variable for that :
SALESFORCE_DB_ALIAS = 'youralias'
But I still have the same problem.
Any recommendation?
Django doesn't support it and an external reference to Salesforce should be currently saved as a CharField and a reference to other databases as IntegerField.
Django docs about Limitations of multiple databases:
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
I tried the cross reference with sqlite as 'default' database. It was possible to create an object of model DJModel with cross-database reference from sqlite to Salesforce. It behaves similarly to normal Django cross-database references, without obscure errors and only a dot reference can be used.
EDIT: Simplified after many years.
I'm using Django 1.4 and experiencing an unusual problem with hidden rows in my admin site.
I can view individual models in my app, and they appear to be querying the database correctly when I click into each one; I can see the number of matches in the top left corner ("3 locations" below my actions bar, for example). However, entries do not appear below that count.
If I try to open an entry directly using the URL, I also get a blank page.
I haven't customized the templates or css. Anyone have experience with this type of problem?
Update: if foreign keys are removed in the model, the entries reappear below the counts. However, that FK field is needed so still trying to identify the root cause.
Fixed. This appears to happen when the model uses a ForeignKey field, and the table it points to does not have its data loaded. That accounts for the model seeing the number of rows, but not being able to pull up the individual entries.
I had this same issue (i.e. I could see the model count in the admin, but not the actual records).
I have a model (Category) with a ForeignKey to another model (Competition). I looked in the database and somewhere along the line while doing some code refactoring on the models and migrations, I ended up with the FKs on the Categories pointing to a non-existent Competition.
When I updated these FKs in the database, the records reappeared in the admin.
In admin.py where you use foreign key you have to add at the last _id for example my foreign key I'd is iClientId so in admin.py we have to write in list_display iClientId_id.
I inserted through SQL (manage.py dbshell) a handful of records. I then went to view the records in the Django admin interface. All of the records appeared, as expected.
However, any queries via Django (not direct SQL), did not return the records. Only after I went to a record through the admin interface, and clicked Save did the record appear in the query.
I'm new to Django, but after repeated searching and reading Django documentation, I did not find an answer. Any idea what is going on, and what can be done to make the SQL INSERT statements appear in queries without visiting each record to Save it through the admin interface?
Maybe you have to declare the transaction "dirty":
from django.db import transaction
transaction.set_dirty()