我正在启动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
字段'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)
我不知道该怎么办,任何人都可以给我一些参考或建议吗? 你的时间。
答案 0 :(得分:1)
如果您需要取消对该行的评论,则需要在该行前添加逗号,。
class JobAdmin(admin.ModelAdmin):
fieldsets = [(None, {'fields': ['pub_date']},
{'fields': ['job_title']},
{'fields': ['job_description']},),
('More info', {'fields': ['location']},)
]
答案 1 :(得分:1)
第9行末尾缺少','(逗号)。