3

I have a model where I have the following architecture

  • There is Post model
  • And there is an answer model for the given post (just like in stack overflow)

Since there are some fields which I do not want the user to populate, I have made a custom form for both of the models. The custom form class is working absolute for the first model i.e. Post Model, but I am getting this strange for the PostAns model. If I remove the class PostAnsForm then it works fine.

class PostAns(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    creator = models.ForeignKey(User, blank=True, null=True)
    post = models.ForeignKey(Post)
    body = models.TextField()
    like_count = models.IntegerField(default=0,blank=True,null=True)
    # If we use markdown can remove this if not
    body_html = models.TextField(blank=True)

class PostAnsForm(ModelForm):

    class Meta:
        model = PostAns
        fields = ('body')

I haven't found a similar error on the net.

Sachin
  • 3,672
  • 9
  • 55
  • 96
  • Possible duplicate of [Django: Field Error Unknown fields](https://stackoverflow.com/questions/6768194/django-field-error-unknown-fields) – Dan Jun 16 '17 at 13:45

1 Answers1

11

the fields attribute is expecting a list or tuple. it finds a string, which is also iterable, but iterating through a string yields each character.

try

fields = ('body', )

this is a fairly common case, and the exact error depends on your string, so is hard to google. the hint is the list of fields (did you possibly change the order of the characters?)

Unknown fields b, o, d, y

notice how they are all one character, and spell out what looks like a string value from your code

second
  • 28,029
  • 7
  • 75
  • 76