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

Add example for admin #28

Open
throwable-one opened this issue Mar 3, 2018 · 0 comments

Comments

@throwable-one
Copy link

@throwable-one throwable-one commented Mar 3, 2018

Hello.

I have the following layout

class Spam(models.Model)
   pass

class Image(models.Model):
   spam = models.ForeignKey(Spam)
   image = models.ImageField()

I now want to add several images to Spam in admin. Plain Django suggests me to use StackInline, but I can't use multiupload here. Here is good recipe you can add to manual


class SpamForm(ModelForm):
    additional_images = MultiFileField(min_num=1, max_num=3,required=False)

    class Meta:
        model = models.Event
        fields = "__all__"

     def save(self, commit=True): # It may be better to do that in  save_m2m for newly created objects
        save = super().save(commit)
        for image in self.cleaned_data["additional_images"]:
                saved_image = models.Image(event=save, additional_image=image)
                saved_image.save()
                save.additional_images.add(saved_image)
        return save



class ImageAdmin(admin.StackedInline):
    model = models.Image

@admin.register(models.Spam)
class EventAdmin(admin.ModelAdmin):
    form = SpamForm
    inlines = [ImageAdmin]

I now can add several images at once using separate form field:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.