samirelanduk / django-random-id-model

A model base class which creates long, random, integer primary keys.
7 stars 2 forks source link

ID field in admin #1

Closed gurugeek closed 3 years ago

gurugeek commented 3 years ago

thanks for your package. I did as per readme 👍

from django_random_id_model import RandomIDModel

class Work(RandomIDModel):
    title = models.CharField(max_length=200)
    body = HTMLField()
    published = models.BooleanField(default=False, null=True)
    date = models.DateTimeField(auto_now=True)

the problem is that now in adming (or add forms) an ID is required so I need to enter it mantually? I am confident that this was not interended behaviour so thanks in advance for your help!

samirelanduk commented 3 years ago

Hi - sorry this is an issue I came across too, I just forgot to add the solution to the README. You just need to to exclude the field in the ModelForm:

class WorkForm(ModelForm):

    class Meta:
        model = Work
        exclude = ["id"]

This should make the form work properly. I haven't used the RandomIDModel with django admin yet, but I suspect this should make it work there too.

If you can confirm this fixes it, I will update the README.

gurugeek commented 3 years ago

thanks for your prompt reply, this fixes it for the forms mode (class meta) l but how to handle it in admin ? it still shows if I add it via admin

samirelanduk commented 3 years ago

Hmmm - it's been a while since I've used admin in depth, but I believe you can specify fields to be excluded there too, using something like this. I think if you exclude the id field there too it should work with admin too.

gurugeek commented 3 years ago

thanks for the reminder, yes this works fine so for admin you need something like

class WorkAdmin(admin.ModelAdmin):
    exclude = ('id',)

admin.site.register(Work,WorkAdmin)

thanks again for this very useful package, much appreciated. The django UUID always had some issues!