Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django dropdown list of users

I am attempting to create an issue tracker similar to github’s. I’m stuck on trying to implement the assigning feature, here’s what I have so far

class Assign(models.Model):
    name = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
    

class Issue(models.Model):
    MARK_AS = ((True, 'Open'), (False, 'Closed'))

    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    assignee = models.ForeignKey(Assign, on_delete=models.SET_NULL, null=True)
    mark_as = models.BooleanField(choices=MARK_AS, default=True)
    

    def __str__(self):
        return self.title
    

    def get_absolute_url(self):
        return reverse('issue-detail', kwargs={'pk': self.pk})

admin page
I feel like I’m close, however when I try to use

class Assign(models.Model):
    name = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
    
    def __str__(self):
        return self.name

I get the error of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

__str__ returned non-string (type User)

Anyone ideas on how to fix this? Thank you.

>Solution :

ERROR DESCRIPTION

str returned non-string (type User) means the Object representation method i.e Assign.__str__() method should always return a string and not an Object like you have returned there.

SOLUTION

Change you Assign Model str() method to be like this.

def __str__():
   return self.name.username

Note:

You should not always return self.user.username like I did there, but literally you can return any field you have in a User model with CharField property.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading