私は得ています
coercing to Unicode: need string or buffer, int found
Django admin でユーザー プロファイルを表示しようとすると。他のオブジェクト タイプでは、このエラーは返されません。エラーが発生しています{{ field.field }}
何か案は?
<div class="controls">
{% if field.is_readonly %}
<p>{{ field.contents }}</p>
{% else %}
{{ field.field }}
{% endif %}
{% if not field.is_readonly and field.errors %}<span class="help-inline">{{ field.errors|striptags }}</span>{% endif %}
{% if field.field.help_text %}
<span class="help-block">{{ field.field.help_text|safe }}</span>
{% endif %}
</div>
以下のコードは、関連情報のみを含めるように変更されています。
ユーザー プロファイル モデル
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
created = models.DateTimeField(auto_now_add=True)
teams = models.ManyToManyField(Team, null=True, blank=True)
projects = models.ManyToManyField(Project, null=True, blank=True)
entries = models.ManyToManyField(Entry, null=True, blank=True)
def __unicode__(self):
return self.user.username
チームモデル
class Team(models.Model):
name = models.CharField(max_length=30, default="Team", unique=False)
created = models.DateTimeField(auto_now_add=True)
projects = models.ManyToManyField(Project, null=True, blank=True)
members = models.ManyToManyField(User)
slug = models.SlugField(max_length=30, unique=False)
def save(self, *args, **kwargs):
if not self.id:
# Newly created object, so set slug
self.slug = slugify(self.name)
super(Team, self).save(*args, **kwargs)
@property
def admin(self):
return self.created_by
def __unicode__(self):
return self.name
プロジェクトモデル
class Project(models.Model):
name = models.CharField(max_length=40, default="Project")
created = models.DateTimeField(auto_now_add=True)
fields = models.PositiveIntegerField(default=5)
entries = models.ManyToManyField(Entry, null=True, blank=True)
slug = models.SlugField(max_length=40, unique=False)
created_by = models.ForeignKey(User, related_name='project_created_by')
members_that_entered = models.ManyToManyField(User, related_name='project_members_that_entered', null=True, blank=True)
members = models.ManyToManyField(User, related_name='project_members', null=True, blank=True)
def save(self, *args, **kwargs):
if not self.id:
# Newly created object, so set slug
self.slug = slugify(self.name)
super(Project, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
エントリーモデル
class Entry(models.Model):
entry = models.TextField(max_length=500)
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, null=True, blank=True)
def __unicode__(self):
return self.entry
入力機能
def add_entry(request, teamslug, projectslug):
user = request.user
try:
project = Project.objects.get(
id=request.POST.get("project_id"))
entries = request.POST.getlist("entry")
team = Team.objects.get(
id=request.POST.get("team_id"))
except Project.DoesNotExist:
messages.warning(request,
"Looks like something went wrong. Try again.")
return HttpResponseRedirect(reverse('project_url',
args=(teamslug, projectslug, team.id, project.id)))
realentries = []
for e in entries:
if e != '':
realentries.append(e)
for e in realentries:
entry = e
entry, created = Entry.objects.get_or_create(
entry=entry, user=user)
project.entries.add(entry)
if not user in project.members_that_entered.all():
project.members_that_entered.add(user)
# remove entries. count amount of entries
# by user in project. if none, remove.
project.save()
profile = request.user.get_profile()
profile.entries.add(entry)
profile.entered_in.get_or_create(
project=project, team=team)
profile.save()
return HttpResponseRedirect(reverse('project_url',
args=(teamslug, projectslug, team.id, project.id)))