Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:39:33+00:00 2026-05-26T02:39:33+00:00

I have posts and attachments. The source of my problem is that I would

  • 0

I have posts and attachments. The source of my problem is that I would like it to be possible to create attachments before creating the post they are attached to (because the user uses the fancy Ajax upload widget to upload the attachment before filling out the details of the post). The solution I have arrived at is to give each post a UUID. The UUID is generated when the post’s modelform is instantiated (i.e., before creating the post). When attachments are uploaded, they are associated with this UUID. Before I pose my question, here is a sketch of the code in order to show more precisely what is going on:

 # models.py

 import uuid
 from django.db import models

 class Post(models.Model):
      nonce = models.CharField(max_length=36)

 class FooPost(Post):
      body = models.TextField()

 class UploadedFile(models.Model):
      url = models.URLField(max_length=1024)
      nonce = models.CharField(max_length=36)

      @classmethod
      def get_unique_nonce(cls):

           while True:
                nonce = str(uuid.uuid4())
                if not cls.objects.filter(nonce=nonce).exists():
                     return nonce



 class Attachment(UploadedFile):
      post = models.ForeignKey(Post)

 # views.py

 class FooPostForm(forms.ModelForm):

      def __init__(self, *args, **kwargs):
           super(PostForm, self).__init__(*args, **kwargs)
           self.initial.setdefault('uuid', UploadedFile.get_unique_uuid())

      def save(self, *args, **kwargs):
           obj = super(FooPostForm, self).save(*args, **kwargs)
           if kwargs.get('commit', True):
                for file in UploadedFile.objects.filter(nonce=obj.nonce)
                     Attachment(uploadedfile_ptr=file, post=obj).save_base(raw=True)
           return obj

      class Meta:
           model = FooPost

 def foo_post(request):
      assert request.method == 'POST'
      form = FooPostForm(request.POST)
      if form.is_valid():
           post = form.save()
      # ...

This works well in my app, but I would like it to also work in the admin interface. How can I inline the attachments in the admin form for the post, even though they are linked indirectly by shared nonce instead of by a foreign key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:39:34+00:00Added an answer on May 26, 2026 at 2:39 am

    Provide a custom modelform for the inline:

     class AttachmentForm(django.forms.ModelForm):
    
         def save(self, *args, **kwargs):
             obj = super(AttachmentForm, self).save(*args, **kwargs)
             obj.nonce = self.instance.nonce
             if kwargs.get('commit', True):
                 obj.save()
             return obj
    

    Exclude the nonce field from the inline:

     class AttachmentInline(admin.TabularInline):
    
         exclude = ['nonce']
         model = Attachment
         form = AttachmentForm
    

    Come up with a new nonce for a new post:

     class PostAdminForm(django.forms.ModelForm):
         def __init__(self, *args, **kwargs):
             super(PostAdminForm, self).__init__(*args, **kwargs)
             self.initial.setdefault('nonce', S3File.get_unique_nonce())
    

    Use the inline:

     # A particular kind of post
     class FooPostAdmin(admin.ModelAdmin):
         form = PostAdminForm
         inlines = [AttachmentInline]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of old posts on a blog that I would like
I have three tables: Post, Attachment, and Media. Posts have Attachments, and Attachments have
I have seen posts that show three ways to reset the DMV views: Reset
I have read posts like these: What is a metaclass in Python? What are
I have seen posts all over the internet that talk about how to fix
I need to retreive a list of posts that have (at least) one attachment
For example I have in my app model User, that has_many models Post. And
I have a model called posts, and it has many attachments. The attachments model
This is what I have in the loop that grabs the posts and displays
i have users table and i have posts table i want select from users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.