简体   繁体   中英

Django ListData on DetailView from Array

here my models.py

class Absent(models.Model):
    ...
    ListOfDate = models.TextField(blank=True, null=True, default='Nothing')
    ...

in my case ListOfDate data will look like this:

[datetime.date(2018, 4, 18), 
 datetime.date(2018, 5, 15), 
 datetime.date(2018, 5, 16), 
 datetime.date(2018, 5, 17)]

I use ListOfDate.append(value) to fill ListOfData .

here my views.py

class Attend(DetailView):
    context_object_name = 'attends'
    model = models.Attend

on ListView commonly use

  {% for attend in attends %}
    <td>{{ attends.ListOfDate }}</td>
  {% endfor %}

for showing list of data. but it will be all in one column. like this:

[datetime.date(2018, 4, 18), 
 datetime.date(2018, 5, 15), 
 datetime.date(2018, 5, 16), 
 datetime.date(2018, 5, 17)]

in my case I want to list ListOfDate like above but not in one column. Like this:

+-----+--------------------------------+
| #   | Date                           |
+-----+--------------------------------+
| 1   | [datetime.date(2018, 4, 18),   |  
|     |  datetime.date(2018, 5, 15),   |
|     |  datetime.date(2018, 5, 16),   |
|     |  datetime.date(2018, 5, 17)]   |
+-----+--------------------------------+

           |
           |
           v

+-----+----------------+
| #   | Date           |
+-----+----------------+
| 1   | 2018-4-18      |
+-----+----------------+
| 2   | 2018-5-15      |
+-----+----------------+
| 3   | 2018-5-16      |
+-----+----------------+
| 4   | 2018-5-17      |
+-----+----------------+

TextField s, CharField s, are typically only used to store, well text. Yes there are some special subclasses to store for example JSON objects and IP addresses, but typically this is only a good idea if you do not have to filter on the subparts of these objects, or do not have to treat these as individual subelements. JSON fields are not bad, especially since they introduce a certain dynamic that is hard to represent in a (relational) database, but one should use them with care in a relational database.

Here you basically have two entities: Absent and an AbsentDate , and ther is a one-to-many relation between an Absent and AbsentDate . We can model this with a ForeignKey :

class Absent(models.Model):
    # ...
    pass

class AbsentDate(models.Model):
    
    date = models.DateField()

    class Meta:
        ordering = ['date']

So that means we have constructed something that looks like:

+--------+ 1   N +------------+
| Absent |-------| AbsentDate |
+--------+       +------------+
                 | date       |
                 +------------+

We can then for a certain Absent instance, query the set of related AbsentDate s with:

our_absent_instance.absentdate_set.all()

So we can use this in a template like:

{% for attend in attends %}
  <td>
  {% for attenddate in attend.absentdate_set.all %}
      {{ attends.attenddate.date }}
  {% endfor %}
  </td>
{% endfor %}

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