django-models

Django. How to reduce total number of coupons after each use

时光毁灭记忆、已成空白 提交于 2020-06-27 08:15:07
问题 Hey guys I am learning to develop a ecommerce website, I have a coupon model with used and max_value, which take care of max number of coupon available to use, I want to implement that in my views, such that if the coupon is redeemed more than the max_value(number of coupons), then it should show an error message. Whatever I have tried with my limited knowledge is resulting in errors. How can I increment the 'used' in views? This is in much more understandable way: users(sellers) are able to

Django - flexible models? [closed]

拈花ヽ惹草 提交于 2020-06-27 06:36:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Improve this question I am new to Django and would like to do a simple CMS with it. There should be a model for pages. The users should be able to set a page type and then different fields should be available. For example (simplified - in reality there would be a lot more

Formset in Django Classbased CreateView

时光总嘲笑我的痴心妄想 提交于 2020-06-27 03:57:36
问题 Model class Timetable(models.Model): day = models.CharField(max_length=9,choices=timetable_choices) start = models.IntegerField() end = models.IntegerField() period = models.CharField(max_length=12) Views class Timetableadding(CreateView): model = Timetable fields = ['day','period','start' ,'end'] success_url = '/dashboard' What I need is to process a view similar to following image , NB: I am not good in js so i want a solution without the use of JS 回答1: CREATE FORMS.PY class MyForm

Formset in Django Classbased CreateView

让人想犯罪 __ 提交于 2020-06-27 03:57:05
问题 Model class Timetable(models.Model): day = models.CharField(max_length=9,choices=timetable_choices) start = models.IntegerField() end = models.IntegerField() period = models.CharField(max_length=12) Views class Timetableadding(CreateView): model = Timetable fields = ['day','period','start' ,'end'] success_url = '/dashboard' What I need is to process a view similar to following image , NB: I am not good in js so i want a solution without the use of JS 回答1: CREATE FORMS.PY class MyForm

Value error on Image field when trying to comvert model to dict

丶灬走出姿态 提交于 2020-06-26 14:48:06
问题 I have 2 models, 1st is Garage class Garage(models.Model): name = models.CharField(verbose_name=_('Garage'), max_length=200) @property def cars(self) -> list: from django.forms.models import model_to_dict cars = [] for i in Car.objects.filter(garage=self.id): cars.append(model_to_dict(i)) return cars def __str__(self): return self.name class Meta: verbose_name = _('Garage') My 2nd model is Car, class Car(models.Model): name = models.CharField(verbose_name=_('Car Name'), max_length=200) garage

django - How to copy actual image file from one model to another?

♀尐吖头ヾ 提交于 2020-06-25 22:49:10
问题 I want to copy images from one model to another within the project. Suppose these are my models: class BackgroundImage(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class ProfilePicture(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date

django - How to copy actual image file from one model to another?

落爺英雄遲暮 提交于 2020-06-25 22:49:06
问题 I want to copy images from one model to another within the project. Suppose these are my models: class BackgroundImage(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class ProfilePicture(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date

Django query single underscore behaving like double underscore?

此生再无相见时 提交于 2020-06-25 21:55:31
问题 I made a typo in my code recently and noticed that I got the same behavior, so I was wondering what the difference between single and double underscores are in django queries. >>> underscore = MyModel.objects.filter(foreign_key_id=var) >>> double_underscore = MyModel.objects.filter(foreign_key__id=var) >>> underscore == double_underscore False >>> list(underscore) == list(double_underscore) True I'm not sure what equality method is being used to compare the querysets, but when I convert to

Django query single underscore behaving like double underscore?

两盒软妹~` 提交于 2020-06-25 21:55:18
问题 I made a typo in my code recently and noticed that I got the same behavior, so I was wondering what the difference between single and double underscores are in django queries. >>> underscore = MyModel.objects.filter(foreign_key_id=var) >>> double_underscore = MyModel.objects.filter(foreign_key__id=var) >>> underscore == double_underscore False >>> list(underscore) == list(double_underscore) True I'm not sure what equality method is being used to compare the querysets, but when I convert to

How can I move a field from one model to another, and still retain the data?

微笑、不失礼 提交于 2020-06-24 22:48:30
问题 To simplify, I have a model, say, class Parent(models.Model): field_a = models.CharField(max_length=40) and another model that holds an image field, that ties itself to a parent instance via foreign key: class ParentPicture(models.model): parent = models.ForeignKey(Parent) picture = models.ImageField(upload_to=upload_path) is_used = models.BooleanField(default=False) The original plan was to support multiple pictures for the parent, and only have one in use at any one time, but now, I'd like