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 7598433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:24:23+00:00 2026-05-30T22:24:23+00:00

I have a model like this: class Item(models.Model): code = models.CharField(max_length=200, unique=True) barcode =

  • 0

I have a model like this:

class Item(models.Model):
    code = models.CharField(max_length=200, unique=True)
    barcode = models.CharField(max_length=300)
    desc = models.CharField('Description',max_length=500)
    display_desc = models.CharField('Description',max_length=500,
                                    blank=True, null=True)
    price = models.FloatField()
    discountable = models.BooleanField(blank=True, default=False)
    image_path = models.CharField(max_length=300,unique=True, blank=True, null=True)    

    def __unicode__(self):
        return self.code + ' : ' + self.desc

But unfortunately, I don’t want to store the item’s image in the database, instead I want to store the image path in the server in the image_path column.

So, I created a custom admin.py for this object so that I could edit/insert the object thru the Django admin module. As a result, below is the customized admin.py

class ItemAdminForm(forms.ModelForm):
    file_upload = forms.FileField(required=False)

    class Meta:
        model = Item

    def __init__(self, *args, **kwargs):
        super(ItemAdminForm, self).__init__(*args,**kwargs)

        #if kwargs.has_key('instance'):
        #    instance = kwargs['instance']
        #    self.initial['file_upload'] = instance.file_upload

    def handle_uploaded_file(f):
        destination = open('D:/Project/pensel/penselshop/static/picture', 'wb+')
        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()
        return f.name

    def save(self,commit=True):
        name = None
        extension = None
        #error here! How could I get the request?
        miniform = ItemAdminForm(request.POST, request.FILES)
        if miniform.is_valid():
            name = handle_uploaded_file(request.FILES['file_upload'])    
            extension = name.split('.')[1]
        model = super(ItemAdminForm, self).save(commit=False)
        model.image_path = '/static/picture/' + model.code + extension        

        if commit:
            model.save()

        return model

However, during processing the save() function, I noticed that there is an error in getting the request. How can I get the request so that I could retrieve the file? I noticed that the request is automatically added in views.py, but not admin.py

  • 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-30T22:24:24+00:00Added an answer on May 30, 2026 at 10:24 pm

    Django’s ImageField and FileField fields don’t actually store the image in the database either. All that is stored in the database is the path, which you can control yourself. The actual image file is stored on the filesystem. So I’m not sure why you are going to all this trouble…?

    But to answer your question of how to get the request in the admin, you can override ModelAdmin.save_model().

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

Sidebar

Related Questions

I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
I have the following models: class Product(models.Model): active = models.BooleanField(default=True) name = models.CharField(max_length=40, unique=True)
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
I have a report model looking a bit like this: class Report(models.Model): date =
I have the following models (simplified): class Location(models.Model): name = models.CharField(max_length=100) is_ok = models.BooleanField()
I have a model class like this: namespace Models { public struct Localization {

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.