Home > Back-end >  Django ManyToMany table output
Django ManyToMany table output

Time:07-25

i'm having a bit of trouble trying to return the actual value of a seperate model, i would like to return simple 'in progress' or 'open' or whichever states i add. Instead it's showing <queryset and this is only viewable when i add the .all on the end, otherwise it states None (or is blank).

enter image description here

the status does bring back the correct data, however i just need it to say the result OPEN or IN progress and get rid of the queryset- a workflow can go through multiple status's throughout its lifecycle

Views.py

def workflows(request):
    return render(request, 'workflows/workflows.html', {
    'workflows': Workflow.objects.all()
  })

models.py

class Workflow(models.Model):
    id = models.AutoField(primary_key=True, editable=False)
    name = models.CharField(max_length=50)
    description = models.TextField(null=True, blank=True, max_length=30)
    status = models.ManyToManyField('Status', blank=True,  related_name='workflows')
    created = models.DateTimeField(auto_now_add=True)
    # tags = models.ManyToManyField('Tag', blank=True)
    modified = models.DateTimeField(auto_now_add=True)
    # created_by = 
    # requester_id
    retired = models.BooleanField(default=False)
    
    def __str__(self):
        return self.name

    
        #foreign key 1 to many relationship
class Review(models.Model):
    #owner = 
    workflow = models.ForeignKey(Workflow, on_delete=models.CASCADE)
    body = models.TextField(null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    id = models.AutoField(primary_key=True, editable=False)
    
    def __str__(self):
        return f'workflow: {self.workflow} {self.created}'        
    
#many to many relationship
# Tag to be easily identifiable.
class Tag(models.Model):
    name = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)
    id = models.AutoField(primary_key=True, editable=False)
    
    def __str__(self):
        return self.name
    
   # i want the status/states to be selectable 
class Status(models.Model):
    states = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    id = models.AutoField(primary_key=True, editable=False)
    
    def __str__(self):
        return self.states

workflow.html

  {% for workflow in workflows %}
            <tr>
              <td>{{ workflow.id }}</td>
              <td>{{ workflow.name }}</td>
              <td>{{ workflow.description }}</td>
              <td>{{ workflow.status.all }}</td>
              <td>{{ workflow.created }}</td>
              <td>{{ workflow.modified }}</td>
              <td>{{ workflow.retired }}</td>

forms.py

class WorkflowForm(forms.ModelForm):
  class Meta:
    model = Workflow
    fields = '__all__'

Thank you kindly.

CodePudding user response:

You want to just use <td>{{ workflow.status.states }}</td> instead. If it returns None then there's no entry in the database for this column and item. Else it returns the state for that status

  {% for workflow in workflows %}
            <tr>
              <td>{{ workflow.status.states }}</td>
        ..
        ..
  • Related