Anon
Anon

Reputation: 5163

Writing file line to Django model field

I can't seem to write a line from a file to a field of a Django model. The field is described in the model as:

 text = models.TextField(null=True, blank=True, help_text='A status message.')

However, when I attempt to create a new object I cannot fill this field using the readline function:

file = open(filename, 'r')
str = file.readline()

When I attempt to use str for the text field I don't seem to be able to read or write to the database. I'm not given any error so I'm assuming its an encoding problem. Any advice? Thanks.

edit

The database is a postgres database and the field type is "text".

The code I'm using to create the object is:

Message.objects.create_message(sys, str)

and

def create_message(self, system, msg):
        """Create a message for the given system."""
        m = Message.objects.create(system=system,
                   text=msg)
        return m

Upvotes: 0

Views: 173

Answers (2)

Johannes Charra
Johannes Charra

Reputation: 29913

I think you're not calling the create_message method properly. Shouldn't you just call it like this

create_message(sys, str)

instead of this

Message.objects.create_message(sys, str)

?

Upvotes: 0

Piotr Duda
Piotr Duda

Reputation: 1815

Don't create variable names which conflict with python built in types. "str" is the string type.Python interpreter:

>> str
<type 'str'>

Upvotes: 2

Related Questions