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

  • Home
  • SEARCH
  • 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 173287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:19:01+00:00 2026-05-11T13:19:01+00:00

I’d like to save my files using the primary key of the entry. Here

  • 0

I’d like to save my files using the primary key of the entry.

Here is my code:

def get_nzb_filename(instance, filename):     if not instance.pk:         instance.save() # Does not work.     name_slug = re.sub('[^a-zA-Z0-9]', '-', instance.name).strip('-').lower()     name_slug = re.sub('[-]+', '-', name_slug)     return u'files/%s_%s.nzb' % (instance.pk, name_slug)  class File(models.Model):     nzb = models.FileField(upload_to=get_nzb_filename)     name = models.CharField(max_length=256) 

I know the first time an object is saved the primary key isn’t available, so I’m willing to take the extra hit to save the object just to get the primary key, and then continue on.

The above code doesn’t work. It throws the following error:

maximum recursion depth exceeded while calling a Python object 

I’m assuming this is an infinite loop. Calling the save method would call the get_nzb_filename method, which would again call the save method, and so on.

I’m using the latest version of the Django trunk.

How can I get the primary key so I can use it to save my uploaded files?


Update @muhuk:

I like your solution. Can you help me implement it? I’ve updated my code to the following and the error is 'File' object has no attribute 'create'. Perhaps I’m using what you’ve written out of context?

def create_with_pk(self):     instance = self.create()     instance.save()     return instance  def get_nzb_filename(instance, filename):     if not instance.pk:         create_with_pk(instance)     name_slug = re.sub('[^a-zA-Z0-9]', '-', instance.name).strip('-').lower()     name_slug = re.sub('[-]+', '-', name_slug)     return u'files/%s_%s.nzb' % (instance.pk, name_slug)  class File(models.Model):     nzb = models.FileField(upload_to=get_nzb_filename, blank=True, null=True)     name = models.CharField(max_length=256) 

Instead of enforcing the required field in my model I’ll do it in my Form class. No problem.

  • 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. 2026-05-11T13:19:02+00:00Added an answer on May 11, 2026 at 1:19 pm

    It seems you’ll need to pre-generate your File models with empty file fields first. Then pick up one and save it with the given file object.

    You can have a custom manager method like this;

    def create_with_pk(self):     instance = self.create()     instance.save()     # probably this line is unneeded     return instance 

    But this will be troublesome if either of your fields is required. Because you are initially creating a null object, you can’t enforce required fields on the model level.

    EDIT

    create_with_pk is supposed to be a custom manager method, in your code it is just a regular method. Hence self is meaningless. It is all properly documented with examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.