I have a model and originally it had these attributes
First Name
Last Name
Email
I altered the model to include an additional attribue : Address , now it looks like this
First Name
Last Name
Email
Address
I am using MySQL database and the changes are reflected in the table in the database , however the changes are not reflected in Django admin as the tables does not have a new column called Address.
I know it has something to do with overriding the admin template in Django but i cant seems to be able to do it , can someone guide me
Thanks
In your admin.py file in your app directory, there will be a subclass of ModelAdmin that is registered for this model. Make sure the address field is listed in either the fields or fieldsets property of this class, and make sure it is not listed in the exclude list on this class.
REF: https://docs.djangoproject.com/en/dev/ref/contrib/admin/
Related
Sorry for my English
I have a table to manage list of database configurations. Table post in all db have same fields but different name. Eg. table post name is md_post, table post in other db is tdt_post.
So, when I access a db configuration I want to remote that database. I did it! But how can I change tb_name in Meta Django Model?
These Datatables configurations are non-permanent and may change when I add/update/delete record.
I tried it, but it only works one time.
Post._meta.db_table = `tdt_post`
Post._meta.original_attrs['db_table'] = `tdt_post`
When I change the db_table back to 'md_post', it doesn't work.
I have looked at the following posts, but it doesn't solve my problem:
Django model: change db_table dynamically,
Change table name for django model in runtime
I have a model with a PointField from django.contrib.gis.db.models . This somehow doesn't let the admin site show the objects with a nice table of fields and values. Instead it displays one field named after the model name. The values are just a bunch of ' object'. With the name of the model. If I click the object I can edit it fine. It would be nice however to be able to filter and see the field values at the admin/ page itself.
Since PointField does not have a __unicode__ attribute, for the proper name to show up, you can register a new admin model object.
Now, in the admin's list_display,
class PointFieldAdmin(admin.ModelAdmin):
list_display = ('name', 'field_x', 'field_y', ...)
admin.register(PointField, PointFieldAdmin)
More on admin models registering here
I'm creating a simple project in Django to further my knowledge of it and Python, essentially just going to store bookmarks and tags, both will have views, and there will be 3 models:
Tag
Bookmark
Bookmark_Tag (relationships)
I read the "Projects vs Apps" panel in the django documentation, but I'm still unsure of how to lay it out, right now it's all within one Bookmark App, should there be a seperate app for Bookmarks and a seperate app for Tags, and if so, where does the model for the relationships live?
Thanks!
No, you don't need a separate app for each. They're closely related, and it sounds like together they define your app, not separately. If later, you added another functionality to your site that used the same database and settings but didn't share much else with your current app, that would be another app in the same project.
See Django project models.py versus app models.py and Django: "projects" vs "apps" on this site as well as Django tips: laying out an application for some more guidelines.
If Bookmarks and Tags have a many-to-many relationship, and you need to add extra fields to that relationship (other than just the ids of the related objects) you can use a ManyToManyField and set the through table:
class Bookmark(models.Model):
# whatever fields you need on Bookmark
tags = models.ManyToManyField('Tag', through = 'BookmarkTag')
class Tag(models.Model):
# whatever fields you need on Tag
pass
class BookmarkTag(models.Model):
bookmark = models.ForeignKey(Bookmark)
tag = models.ForeignKey(Tag)
# whatever additional fields you need on the relationship
See the Django docs on Extra fields on many-to-many relationships for more info.
I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
replace the field with an integer
field I can manually modify to the
contact ID when necessary
replace the dropdown menu with some
alternative input method which won't
crash the browser
remove this input
from the Customer admin page
altogether
Thanks!
You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the raw_id_fields of the admin class.
You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the formfield_for_foreignkey method on the Admin model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.
Sounds like specifying the contact field in raw_id_fields in your admin.py entry for the relevant model would sort you out. Docs are here.
PS. Surprised (but not that surprised) that FF gives out before your database server tanks...
For instance, I have a model called Person, and it has a bool field called 'isAthlete'. I would like to be able to check off True for 50 of these Person records, and then hit submit, without having to go into each Person model record and make the change. Is there an easy or already provided way to set this up in Django?
you can do this using django admin actions, http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/