简体   繁体   中英

Django RelatedManager converts tuple to Unicode string?

To help myself learn Python, I'm writing a simple issue tracker using Django.

I have two simple classes (left some code out for brevity), Issue and Version

There is an ISSUE_STATE tuple that is used to maintain an Issue 's state:

ISSUE_STATE = (
    ('p', 'In Progress'),
    ('o', 'Open'),
    ('r', 'Resolved'),
    ('c', 'Closed'),
)

It's maintained in the Issue like so:

class Issue(models.Model):
    state = models.CharField(max_length=1, choices=ISSUE_STATE)
    fix_version = models.ForeignKey(Version, related_name='issuesAsFix', null=True, blank=True, default=None)

(As you can also see, a Version maintains a list of Issue objects.)

The problem:

When I access the state of an individual Issue instance, it's returned as a tuple. When I access the state of an Issue as provided by a Version object, it's returned as a Unicode string:

>>> v = Version()
>>> v.save()

>>> i = Issue()
>>> i.fix_version = v
>>> i.state = ISSUE_STATE[1]
>>> i.save()

>>> i.state
('o', 'Open')
>>> v.issuesAsFix.all()[0].state
u"('o', 'Open')"

>>> i == v.issuesAsFix.all()[0]
True
>>> i is v.issuesAsFix.all()[0]
False

Why is the state variable of the Issue a string when accessed as a child property of the Version ?

Thanks in advance!

 >>> i.state = ISSUE_STATE[1] 

This line is incorrect. It should be:

>>> i.state = ISSUE_STATE[1][0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM