django-models

Django: Add and Subtract from inventory in models

做~自己de王妃 提交于 2020-01-11 14:21:50
问题 I'm trying to do something that I thought would be simple, but it has proven a bit challenging for me right now. I am trying to build a simple ATM system for Banknotes in Django 1.11 and Python 3.6. I basically need to keep track of banknotes that are in stock and how many of each kind. But I realized that using the logic that I'm accustomed with I only create new instances of the model instead of adding to the quantity fields. I know how to use a quantity field to add items of an order (but

Listing only usable values in OneToOneField Django

萝らか妹 提交于 2020-01-11 13:35:29
问题 I want to list only usable items in OneToOneField not all items, its not like filtering values in ChoiceField because we need to find out only values which can be used which is based on the principle that whether it has been used already or not. I am having a model definition as following: class Foo(models.Model): somefield = models.CharField(max_length=12) class Bar(models.Model): somefield = models.CharField(max_length=12) foo = models.OneToOneField(Foo) Now I am using a ModelForm to create

Two annotations in a single query

时光毁灭记忆、已成空白 提交于 2020-01-11 13:34:19
问题 I am trying to build a query set to do a count, the query set involves three models. class Owner(models.Model): name = models.CharField(max_length=10, null=False) class Location(models.Model): name = models.CharField(max_length=10, null=False) owner = models.ForeignKey(Owner, on_delete=models.SET_NULL, null=True, blank=True) class Asset(models.Model): name = models.CharField(max_length=10, null=false) owner = models.ForeignKey(Owner, on_delete=models.SET_NULL, null=True, blank=True) location

Django use a variable within a template tag

感情迁移 提交于 2020-01-11 13:05:25
问题 I am using the static template tag in my Django template: {% load staticfiles %} <img src="{% static "my_app/myexample.jpg" %}" alt="My image"/> But instead of "my_app/myexample.jpg" , I need to use a file name that is a property of my model object (i.e. {{ SampleModel.0.propertyValue }} , which I am passing as context to this template. But how do I include {{ ... }} within the static template tag? It throws an error. Any way out? 回答1: You can use use a variable in the static template tag.

Choose queryset for limit_choices_to based on object fields

社会主义新天地 提交于 2020-01-11 12:05:45
问题 I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't

Choose queryset for limit_choices_to based on object fields

谁说胖子不能爱 提交于 2020-01-11 12:04:51
问题 I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't

Choose queryset for limit_choices_to based on object fields

坚强是说给别人听的谎言 提交于 2020-01-11 12:04:37
问题 I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't

Django - app to build reports using data retrieved from a REST-like API

北城余情 提交于 2020-01-11 05:11:33
问题 I have been building a Django Application which consumes data from an expansive REST-like API. The API was built using .NET (yuck! not my choice), and since I would rather gouge out my eyeballs than learn Microsoft tools (I'm a *nix/OSX guy), and since I do not want the front-end to affect the API or vice-versa, I elected to build the front-end using Django on another server. The Django site acts as a middleman between the main DB/API and the End-User. None of the data from the API is

Django teacher students easy solution. Use separate tables, or permissions and groups? How? Other ideas?

隐身守侯 提交于 2020-01-11 04:06:06
问题 How would you cope with following problem? There are teachers, and students. Student can not view pages dedicated to teachers, and teachers can view pages dedicated to students. Teachers can have list of just students they teach. I want both teachers and students to use build in User to let them login. I have following ideas: Separate table for teacher and student with foreign key to build in user? - but the question is, can I easily render pages and distinguish who is teacher and who is

get class name for empty queryset in django

坚强是说给别人听的谎言 提交于 2020-01-10 20:15:10
问题 I have empty queryset of model Student students = Students.objects.all() If the above queryset is empty, then how can i get the model(class name)? How can i get the model name for empty queryset? EDIT: How can i get the app name from the queryset? 回答1: >>> students = Students.objects.all() # The queryset's model class: >>> students.model project.app.models.Student # Name of the model class: >>> students.model.__name__ 'Student' # Import path of the models module: >>> students.model.__module__