Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using models.DateField with auto_now_add = True wont show in page props #65

Open
buffmomoeveryday opened this issue Jan 18, 2025 · 1 comment

Comments

@buffmomoeveryday
Copy link

Hi I'm encountering this weird issue where the model's date field with auto_now_add field wont show up in page props, I'm guessing it's some thing to do with the InertiaJson Encoder as I'm new to this I dont know where to start to fix this.

Thank You

class DailyReport(models.Model):
    date = models.DateField(auto_now_add=True)
    issues_challenges = models.TextField(
        blank=True, null=True, help_text="Describe any challenges or blockers"
    )
    additional_notes = models.TextField(
        blank=True, null=True, help_text="Any relevant updates or comments"
    )

    def __str__(self):
        return f"Report by on {self.date}"

    @property
    def get_date(self):
        return self.date
<script setup>
import { Button } from '@/components/ui/button';
import { computed } from 'vue'
import { usePage } from '@inertiajs/vue3'

const page = usePage()

const daily_reports = computed(()=>page.props.daily_report)


</script>

<template>
{{ daily_reports }}

<div v-if="daily_reports">
<li v-for="report in daily_reports">
    <ul>
        Date : {{ report.id }}
    </ul>
</li>
</div>
<div v-else>
No Daily Reports
</div>
</template>

Image

@BrandonShar
Copy link
Collaborator

Under the hood the InertiaJsonEncoder uses Django's model_to_dict function and unfortunately this is an implementation detail of that function. model_to_dict doesn't include any fields that have editable=False and any fields with auto_now_add=True automatically set editable=False.

The best bet here is to convert this model to a dict or json before sending it to inertia.

If you have any ideas of how this could work more fluidly, let me know because I also think this is annoying behavior 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants