How to Drop Trailing 0s for Front-end Display in Django Modelform DecimalFields (Dollar Display)

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024

Комментарии • 6

  • @ImperialRoads
    @ImperialRoads Год назад +1

    very gaming

  • @jacobjayme6280
    @jacobjayme6280 Год назад +2

    10/10 very epic and cool

  • @jeremine9259
    @jeremine9259 Год назад +1

    Hi Daniel, thanks for great content!
    Hope you can make a video on how to achieve the same result with Django's Admin page.

    • @DanielBoctor
      @DanielBoctor  Год назад +2

      Thanks! I'm glad you enjoyed!
      All you need to do for this is head over to your admin.py, and import your form (the same form that you applied the widgets to) with
      *from .forms import YourForm*
      Then create a ModelAdmin with
      *class YourModelAdmin(admin.ModelAdmin): form = YourForm*
      Lastly, register the new form with
      *admin.site.register(YourModel, YourModelAdmin)*
      This is all you need, and the new widget will be applied to your admin form. I just wanted to get back to you quickly, and if you want more depth, I would be happy to make another video.

    • @DanielBoctor
      @DanielBoctor  Год назад +1

      P.S.
      If you want to change anything for the admin form in specific (class variables or methods), go ahead and subclass a new form, and you can change whatever you like to suit your needs.
      *class YourNewForm(YourForm):*
      *class Meta(YourForm.Meta):*
      *# exclude = ()*
      *# or*
      *# fields = '__all__'*
      Don't forget to add this new form to the ModelAdmin instead. Cheers!

    • @jeremine9259
      @jeremine9259 Год назад +2

      Thank you again for your quick response!
      In the meantime, I did also find a solution for my problem and want to share it here in case you or anyone else needs another approach. In the "admin.py" file:
      class YourModelAdmin(admin.ModelAdmin):
      list_display = ['field_1', 'field_2', 'field_3_notrailingzeros']
      def field_3_notrailingzeros(self, obj):
      return format(obj.field_3, 'f')
      field_3_notrailingzeros.short_description = 'field_3 (no trailing 0s)'
      In general, I created a function inside my ModelAdmin to reformat my original field named 'field_3'. After that, in the list_display, I replaced that field with the new function's name. That's it!
      However, I still think you should make a video to introduce your solution as I see that RUclips videos on this topic are quite rare. Look forward to your next useful contents!