Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 20 20:20
    mikicz commented #50
  • May 20 19:28
    kingbuzzman closed #50
  • May 20 19:28
    kingbuzzman commented #50
  • Mar 16 01:46
    aragilar commented #53
  • Feb 07 15:05
    esirK opened #61
  • Nov 22 2021 13:50
    mikicz commented #32
  • Nov 22 2021 13:50
    mikicz commented #39
  • Nov 22 2021 13:50
    mikicz commented #47
  • Nov 22 2021 13:49
    mikicz commented #52
  • Oct 26 2021 20:30
    scott-w removed as member
  • Oct 26 2021 20:30
    tpict removed as member
  • Sep 27 2021 17:11
    andrehenriquemendes closed #59
  • Jul 15 2021 15:24
    kingbuzzman closed #51
  • Jul 04 2021 09:04
    SathishNaqel opened #60
  • May 12 2021 12:22
    mikicz commented #59
  • May 12 2021 12:20
    andrehenriquemendes commented #59
  • May 12 2021 12:18
    andrehenriquemendes commented #59
  • May 11 2021 12:27
    mikicz commented #59
  • May 11 2021 12:16
    andrehenriquemendes commented #59
  • May 11 2021 10:54
    mikicz commented #59
Shane Grey
@shanemgrey
Trying to use pgviews and the app is generating the views correctly when I run sync_pgviews, but DRF only shows a list of the id field for the result of the query even when I use queryset = Report.objects.all() and fields = 'all'
When I request the view data in pgModeler I get all the fields as expected. What am I missing that's causing DRF to only render the ID?
Scott Walton
@scott-w
hi @shanemgrey, do you have a code sample of your model?
Shane Grey
@shanemgrey

``` class HouseholdTargetSummaryView(pgView.View):

# projection = ['target.HouseholdTarget.*',]
# dependencies = ['target.HouseholdTarget', 'target.HouseholdTargetJoin']
sql = """
    SELECT t.*,
           COUNT(e.start_date) AS enrolled,
           COUNT(e.end_date) AS completed,
           CASE WHEN t.enrollment_goal > 0 THEN ROUND((COUNT(e.start_date))::decimal/t.enrollment_goal*100,0) ELSE NULL END AS percent_enrolled,
           CASE WHEN t.completion_goal > 0 THEN ROUND((COUNT(e.end_date))::decimal/t.completion_goal*100,0) ELSE NULL END AS percent_completed
    FROM target_householdtarget t
    INNER JOIN target_householdtargetjoin e ON t.id = e.target_id
    WHERE e.start_date IS NOT NULL
      AND e.active = TRUE
      AND t.active = TRUE
    GROUP BY t.id
    ORDER BY t.calendar_year DESC, t.target_num;
"""

```

class Meta:
  app_label = 'target'
  db_table = 'target_householdtarget_summary'
  managed = False
Is it necessary to create another models.model mapping the fields to the resulting view fields?
Scott Walton
@scott-w
@shanemgrey I think it’s because DRF looks at the fields attached to the Model
Shane Grey
@shanemgrey
what is projection supposed to do?
Scott Walton
@scott-w
does it work if you add the fields as:
class HouseholdTargetSummaryView(View):
    sql = ‘...'
    enrolled = models.DateField()
    completed = models.DateField()
Shane Grey
@shanemgrey
The view I pasted doesn't have it's own model. It's the result of a join of two others. Do I need to create a third model just to map the fields resulting from the query?
Scott Walton
@scott-w
no, the View creates the model for you
Shane Grey
@shanemgrey
I'm confused on where I would add those fields. In the views.py, or the models.py
n/m. Added to the models.py
No change.
Scott Walton
@scott-w
can you paste the code from your DRF serializer?
Shane Grey
@shanemgrey
class HouseholdTargetSummaryReportViewSerializer(CustomBaseSerializer):
  class Meta:
      model = HouseholdTargetSummaryView
      fields = '__all__'
The custombaseserialzer just adds timestamps- Correction, it adds links to the users who created or modified the row
Shane Grey
@shanemgrey
Found my error. I needed to explicitely define all of the model fields.
class HouseholdTargetSummaryView(pgView.View):
    # projection = ['target.HouseholdTarget.*',]
    # dependencies = ['target.HouseholdTarget', 'target.HouseholdTargetJoin']
    sql = """
        SELECT t.*,
               COUNT(e.start_date) AS enrolled,
               COUNT(e.end_date) AS completed,
               CASE WHEN t.enrollment_goal > 0 THEN ROUND((COUNT(e.start_date))::decimal/t.enrollment_goal*100,0) ELSE NULL END AS percent_enrolled,
               CASE WHEN t.completion_goal > 0 THEN ROUND((COUNT(e.end_date))::decimal/t.completion_goal*100,0) ELSE NULL END AS percent_completed
        FROM target_householdtarget t
        INNER JOIN target_householdtargetjoin e ON t.id = e.target_id
        WHERE e.start_date IS NOT NULL
          AND e.active = TRUE
          AND t.active = TRUE
        GROUP BY t.id
        ORDER BY t.calendar_year DESC, t.target_num;
    """

    created_at = models.DateTimeField()
    modified_at = models.DateTimeField()
    target_num = models.IntegerField()
    target_type = models.TextField()
    calendar_year = models.IntegerField()
    description = models.TextField()
    enrollment_goal = models.IntegerField()
    completion_goal = models.IntegerField()
    notes = models.TextField()
    active = models.BooleanField()
    created_by_id = models.IntegerField()
    modified_by_id = models.IntegerField()
    enrolled = models.IntegerField()
    completed = models.IntegerField()
    percent_enrolled = models.IntegerField()
    percent_completed = models.IntegerField()


    class Meta:
      app_label = 'target'
      db_table = 'target_householdtarget_summary'
      managed = False
Which is what I think you were getting at when suggesting I add those fields. :-)
Thank you for the nudge in the right direction!
Scott Walton
@scott-w
no worries dude :)
i’d like to get round to making that work somehow without the fields but it’s never got high up enough in the list :P
Shane Grey
@shanemgrey
I totally understand. I appreciate what you have already accomplished.
Scott Walton
@scott-w
most of the credit has to go to others, I have to admit
i’m merely the shepherd ;)