1

我越来越

coercing to Unicode: need string or buffer, int found

当我尝试在 Django 管理员中查看用户配置文件时。其他对象类型均未返回此错误。错误发生在{{ 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)))
4

0 回答 0