👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Say I have the following in my models.py
:
class Company(models.Model):
name = ...
class Rate(models.Model):
company = models.ForeignKey(Company)
name = ...
class Client(models.Model):
name = ...
company = models.ForeignKey(Company)
base_rate = models.ForeignKey(Rate)
I.e. there are multiple Companies
, each having a range of Rates
and Clients
. Each Client
should have a base Rate
that is chosen from it"s parent Company"s Rates
, not another Company"s Rates
.
When creating a form for adding a Client
, I would like to remove the Company
choices (as that has already been selected via an "Add Client" button on the Company
page) and limit the Rate
choices to that Company
as well.
How do I go about this in Django 1.0?
My current forms.py
file is just boilerplate at the moment:
from models import *
from django.forms import ModelForm
class ClientForm(ModelForm):
class Meta:
model = Client
And the views.py
is also basic:
from django.shortcuts import render_to_response, get_object_or_404
from models import *
from forms import *
def addclient(request, company_id):
the_company = get_object_or_404(Company, id=company_id)
if request.POST:
form = ClientForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(the_company.get_clients_url())
else:
form = ClientForm()
return render_to_response("addclient.html", {"form": form, "the_company":the_company})
In Django 0.96 I was able to hack this in by doing something like the following before rendering the template:
manipulator.fields[0].choices = [(r.id,r.name) for r in Rate.objects.filter(company_id=the_company.id)]
ForeignKey.limit_choices_to
seems promising but I don"t know how to pass in the_company.id
and I"m not clear if that will work outside the Admin interface anyway.
Thanks. (This seems like a pretty basic request but if I should redesign something I"m open to suggestions.)
👻 Read also: what is the best laptop for engineering students?
How do I filter ForeignKey choices in a Django ModelForm? __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
How do I filter ForeignKey choices in a Django ModelForm? __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from How do I filter ForeignKey choices in a Django ModelForm?, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How do I filter ForeignKey choices in a Django ModelForm?
- Deutsch How do I filter ForeignKey choices in a Django ModelForm?
- Français How do I filter ForeignKey choices in a Django ModelForm?
- Español How do I filter ForeignKey choices in a Django ModelForm?
- Türk How do I filter ForeignKey choices in a Django ModelForm?
- Русский How do I filter ForeignKey choices in a Django ModelForm?
- Português How do I filter ForeignKey choices in a Django ModelForm?
- Polski How do I filter ForeignKey choices in a Django ModelForm?
- Nederlandse How do I filter ForeignKey choices in a Django ModelForm?
- 中文 How do I filter ForeignKey choices in a Django ModelForm?
- 한국어 How do I filter ForeignKey choices in a Django ModelForm?
- 日本語 How do I filter ForeignKey choices in a Django ModelForm?
- हिन्दी How do I filter ForeignKey choices in a Django ModelForm?
Milan | 2023-03-24
html Python module is always a bit confusing 😭 How do I filter ForeignKey choices in a Django ModelForm? is not the only problem I encountered. I just hope that will not emerge anymore
California | 2023-03-24
Simply put and clear. Thank you for sharing. How do I filter ForeignKey choices in a Django ModelForm? and other issues with Ev PHP module was always my weak point 😁. I just hope that will not emerge anymore
Boston | 2023-03-24
Simply put and clear. Thank you for sharing. How do I filter ForeignKey choices in a Django ModelForm? and other issues with imp Python module was always my weak point 😁. I just hope that will not emerge anymore