django-admin

Default value for user ForeignKey with Django admin

偶尔善良 提交于 2020-01-10 07:17:09
问题 author = models.ForeignKey(User) Is there any way I can set the default value of this field to automatically select the logged in user? I know you can do this in the save method but my client has requested that it automatically defaults to the logged in user at edit time to avoid confusion. This article goes into how to update it during save, just not at edit time. 回答1: class MyModelAdmin(admin.ModelAdmin): def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name ==

Can't open file 'django-admin.py': [Errno 2] No such file or directory

妖精的绣舞 提交于 2020-01-09 19:46:07
问题 I'm using Python 2.7, Django 1.2.5 and on Windows 7. I am not sure what I've done. I used to be able to create Django projects like python django-admin.py startproject test Now however I get this error. Can't open file 'django-admin.py': [Errno 2] No such file or directory I can type the following which works. python C:\Python27\Scripts\django-admin.py startproject test How can I have it the way it used to be? Not having the type the full path to the django-admin.py file. Things I've already

Django-Admin: CharField as TextArea

匆匆过客 提交于 2020-01-09 08:23:12
问题 I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 ) class Cab_Admin(admin.ModelAdmin): ordering = ('name',) list_display = ('name','descr', ) # what to write here to make descr using TextArea? admin.site.register( Cab, Cab_Admin ) how to assign TextArea widget to 'descr' field in admin interface? upd: In Admin interface only! Good idea to use ModelForm. 回答1: You will have to create a forms.ModelForm that will describe how you

Django: accessing the model instance from within ModelAdmin?

梦想与她 提交于 2020-01-09 06:04:47
问题 I've got a model for Orders in a webshop application, with an auto-incrementing primary key and a foreign key to itself, since orders can be split into multiple orders, but the relationship to the original order must be maintained. class Order(models.Model): ordernumber = models.AutoField(primary_key=True) parent_order = models.ForeignKey('self', null=True, blank=True, related_name='child_orders') # .. other fields not relevant here I've registered an OrderAdmin class for the admin site. For

Django — Conditional Login Redirect

别说谁变了你拦得住时间么 提交于 2020-01-09 04:55:06
问题 I am working on a Django application that will have two types of users: Admins and Users. Both are groups in my project, and depending on which group the individual logging in belongs to I'd like to redirect them to separate pages. Right now I have this in my settings.py LOGIN_REDIRECT_URL = 'admin_list' This redirects all users who sign in to 'admin_list', but the view is only accessible to members of the Admins group -- otherwise it returns a 403. As for the login view itself, I'm just

How to bypass django admin index and go to a specific application page?

社会主义新天地 提交于 2020-01-06 18:12:34
问题 I've got a Django project with only one app. I don't need the site admin (same as my app admin), so I would like to bypass the site admin and go directly to my app admin, i.e. go directly to mysite/admin/myapp/ after loging in and removing Home in the breadcrumb. How can I do that? 回答1: What you can do is override the admin index page to redirect to app specific admin page. As stated by @FallenAngel, you need to update the urls.py to have your view for admin index page. That view can redirect

How to bypass django admin index and go to a specific application page?

风流意气都作罢 提交于 2020-01-06 18:10:34
问题 I've got a Django project with only one app. I don't need the site admin (same as my app admin), so I would like to bypass the site admin and go directly to my app admin, i.e. go directly to mysite/admin/myapp/ after loging in and removing Home in the breadcrumb. How can I do that? 回答1: What you can do is override the admin index page to redirect to app specific admin page. As stated by @FallenAngel, you need to update the urls.py to have your view for admin index page. That view can redirect

When I reset STATIC_URL to Amazon S3/CloudFront for my production app, Django Admin no longer finds its CSS stylesheet

霸气de小男生 提交于 2020-01-06 13:30:30
问题 How do I resolve? I don't want to push a copy of Django Admin's CSS to my static files unless I have to. Is that my only option or ?? 回答1: Once you move your application to production, in addition to setting DEBUG = False in your settings.py , you also have to run collectstatic and then upload these files to your webserver. collectstatic will put all the static files that are required for all installed applications, including the django.contrib apps (like the django admin), into the folder

How to change the display of Django admin when use ManyToManyField

折月煮酒 提交于 2020-01-06 08:33:14
问题 I'm coding a news website.News model has a ManyToManyField foreign key named tag . Here is my News model: class News(models.Model): title = models.CharField(max_length=100, verbose_name='title') category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="cate", blank=True, verbose_name='Category') tag = models.ManyToManyField(Tag, blank=True, verbose_name='Tags') class Meta: verbose_name = "新闻" verbose_name_plural = verbose_name def __str__(self): return self.title Here is

Django admin - how to get object deletion link in change list?

為{幸葍}努か 提交于 2020-01-06 05:43:09
问题 Is there any possibility to get object delete url in Django Admin, in change list template (change_list_results.html)? I must add icons (edit/delete) on results list and try to get that links for object. 回答1: The delete url for a particular instance can be obtained via: info = obj._meta.app_label, obj._meta.module_name reverse('admin:%s_%s_delete' % info, args=(obj.id,)) So, just add a method to your ModelAdmin to return that URL wrapped in appropriate HTML: def delete_link(self, obj): info =