我正在啓動python和django。 我想製作和「添加」頁面以將數據添加到sqlite3。並不只有兩個元素
1. #my admin.py file
2. from django.contrib import admin
3. from jobs.models import Location
4. from jobs.models import Job
5.
6. class JobAdmin(admin.ModelAdmin):
7. fieldsets = [(None, {'fields': ['pub_date']},
8. {'fields': ['job_title']},
9. {'fields': ['job_description']},)
10. # ('More info', {'fields': ['location']},)
12. ]
13.
14. class LocationAdmin(admin.ModelAdmin):
15. list_display = ('city', 'state', 'country')
16.
17. admin.site.register(Job,JobAdmin)
18. admin.site.register(Location, LocationAdmin)
如果我取消註釋第10行,則出現錯誤。
Exception Type: TypeError
Exception Value: 'tuple' object is not callable
Exception Location: /home/python/djproject/jobs/admin.py in JobAdmin, line 10
如果我評論第10行,就像上面那樣有錯誤。
Exception Type: ImproperlyConfigured
Exception Value: 'JobAdmin.fieldsets[0]' does not have exactly two elements.
Exception Location: /usr/lib/python2.6/site-packages/django/contrib/admin/validation.py in validate_base, line 291
fields'location'是一個外鍵。數據庫中Job的字段中沒有數據,我想創建一個添加頁面以將數據插入數據庫。 和我的models.py是:
#my models.py
from django.db import models
class Location(models.Model):
city = models.CharField(max_length=50)
state = models.CharField(max_length=50, null=True, blank=True)
country = models.CharField(max_length=50)
def __str__(self):
if self.state:
return "%s, %s, %s" % (self.city, self.state, self.country)
else:
return "%s, %s" % (self.city, self.country)
class Job(models.Model):
pub_date = models.DateField()
job_title = models.CharField(max_length=50)
job_description = models.TextField()
location = models.ForeignKey(Location)
def __str__(self):
return "%s (%s)" % (self.job_title, self.location)
我不知道該怎麼做,任何一個可以給我一些參考或建議? thx爲你的時間。
我已經解決了這個問題的端部(逗號)。我發現「list_display」也可以創建添加頁面。爲什麼?因爲JobAdmin和LocationAdmin是從ModelAdmin繼承的? –