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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:38:22+00:00 2026-05-13T15:38:22+00:00

And i have a simple modelform for Package from models import Package from django

  • 0

And i have a simple modelform for Package

from models import Package
from django import forms

class PackageForm(forms.ModelForm):
class Meta:
    model= Package
    fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file']

How can i ask the modelform to check, within validation, if the file extension (class is FileField) is .sh for example?

is there a way to put this in modelform? of can i only manage it in a view?

Thanks

Edit:
Also, forgot to ask, the model has a Foreignkey to the auth User model… which is going to contain the current user.. how can modelform manage that?

Thanks again


Thanks for the answer! i’m getting hold of this.. although i encounter a problem

Package contains a foreignkey to django.contrib.auth.models User model,
When the form is processed how can i tell the modelform to pass the current user object to the model instance?
i thought of this…

views.py

def add(request):
if request.method == 'POST':
    the_model= PackageForm(request.user, request.POST, request.FILES)
    if the_model.is_valid():
        the_model.save()

i overwrited the init in modelform:

from models import Package
from django import forms

class PackageForm(forms.ModelForm):
def __init__(self,user,*args,**kwargs):
        super (PackageForm,self ).__init__(*args,**kwargs) # populates the post
        self.fields['maintainer_name'].queryset = user # adds the user object passed by add in views.py
class Meta:
    model= Package
    fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file']

manteiner_name is the ForeignKey(User) object…
it gives me a keyerror 🙁 that’s not good…
Any solutions?

Thanks!

  • 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-13T15:38:22+00:00Added an answer on May 13, 2026 at 3:38 pm

    You should read the Django documentation on doing extra validation:

    You just need to define a clean_file() method.

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

Sidebar

Related Questions

I have a class Book : from django.db import models from users.models import User
I am trying to use ModelForm: from django.db import models from django.forms import ModelForm
I've created a simple model class Person(models.Model): name = models.CharField(max_length=25) surname = models.CharField(max_length=25) birth_date
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
I have models similar to the following: class Band(models.Model): name = models.CharField(unique=True) class Event(models.Model):
May be a simple question, but if I have a ModelForm: class ExampleModelForm(forms.ModelForm): class
Background : I have a simple model form with a image field. class XYZForm(ModelForm):
I have a sample form: class AdminDiscountForm(ModelForm): class Meta: model = Discount exclude =
I have dozens of Models, each with ONE associated ModelForm (whose Meta.model refers to
Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model):

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.