django-models

Making sure a field is always null in Django

牧云@^-^@ 提交于 2020-01-06 18:34:50
问题 I have a parent class which is being extended by 3 different models. The parent class has a field called foo lets say, and this field needs to be always null for one of the sub-classes. How can I ensure this? Right now, I am using null=True , and editable=False constraints. However, these can be circumvented from the shell or an API if the field is exposed during object creation. class ThirdSubclass(ParentClass): # Over-ridden field from abstract parent class foo = models

Django, create multiselect checkbox input

我的未来我决定 提交于 2020-01-06 15:18:14
问题 I have a model with an integer field and would like to have an alternative form input render other than the default textbox, so that I have the following: models.py: myfield = models.IntegerField(default=7, blank=True) Would like to have the following: [x] Choice A (value 0) [ ] Choice B (value 1) [x] Choice C (value 2) So that upon save the choices would be calculated like the the following: (since choice A and C are selected and 2 is not selected). myfield = sum(choice selected multiplied

Django: delete M2M orphan entries?

我怕爱的太早我们不能终老 提交于 2020-01-06 14:36:01
问题 I'm porting an existing database application over to Django (so much better!), and have created Django models as follows: class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey(Author) subject = models.ManyToManyField(Subject, related_name='subject') class Author(models.Model): name = models.CharField(max_length=200) class Subject(models.Model): name = models.CharField(max_length=200) I've populated the models from existing data. The problem is that the

Django: delete M2M orphan entries?

徘徊边缘 提交于 2020-01-06 14:34:15
问题 I'm porting an existing database application over to Django (so much better!), and have created Django models as follows: class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey(Author) subject = models.ManyToManyField(Subject, related_name='subject') class Author(models.Model): name = models.CharField(max_length=200) class Subject(models.Model): name = models.CharField(max_length=200) I've populated the models from existing data. The problem is that the

How to pass currently logged in user to filter.py i.e request based Filtering in django

自古美人都是妖i 提交于 2020-01-06 14:19:37
问题 I want to restrict the views for each user i.e a user can view only those account details that are related to him only. For this i have created a Filter where i want to pass the Logged in User details. Below is the snapshot of filter.py class networkFilterUserbased(django_filters.FilterSet): def __init__(self, *args, **kwargs): super().__init__(*args,**kwargs) request = kwargs['request'] username = request.user.id my_choices = NetworkRelatedInformation.objects.values_list('account', 'account_

Django get class from string

心已入冬 提交于 2020-01-06 11:34:13
问题 I'm looking for a generic way in Python to instantiate class by its name in similar way how it is done in Java without having to explicitly specify the class name in IF..ELIF condition. This is because I have several different models and serializers and want to make them addressable by parameters in the HTTP request. It is to enhance loose coupling and modularity. For example https://www.domain.com/myapp/sampledata.json?model=<modelname> should get the classes <modelname> and <modelname

How to change the display of Django admin when use ManyToManyField

折月煮酒 提交于 2020-01-06 08:33:14
问题 I'm coding a news website.News model has a ManyToManyField foreign key named tag . Here is my News model: class News(models.Model): title = models.CharField(max_length=100, verbose_name='title') category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="cate", blank=True, verbose_name='Category') tag = models.ManyToManyField(Tag, blank=True, verbose_name='Tags') class Meta: verbose_name = "新闻" verbose_name_plural = verbose_name def __str__(self): return self.title Here is

Django admin site does not pick current image URL

泄露秘密 提交于 2020-01-06 08:09:27
问题 I am trying to edit the fields of one of my objects in the Django admin site, I also have a Cloudinary image field in my model. The issue is, anytime I try to make an edit to one of the CharField s of my object, I get the error: value too long for type character varying(100) which I later found out that every time I finish my edits and I am trying to save, it looks for a new image to replace the current image of my imagefile even though I did not touch my imagefile , thus it returns an empty

RuntimeWarning for DateField in Django

北城以北 提交于 2020-01-06 07:56:19
问题 Hello i'm using dates for search queries. But i am getting runtime error. RuntimeWarning: DateTimeField Jobs.job_created_on received a naive datetime (2019-01-17 00:00:00) while time zone support is active. Views.py class JobListView(LoginRequiredMixin, generic.TemplateView): template_name = 'admin/jobs/job.html' def get(self, request, *args, **kwargs): context = super(JobListView, self).get_context_data(**kwargs) if 'status' in request.GET: form = JobSearchForm(request.GET) if form.is_valid(

Django models access outside Django without access to the settings file

拜拜、爱过 提交于 2020-01-06 07:27:05
问题 I would like to allow people that don't have access to the Django project root folder (so no access to settings.py file and no DB password) to use specific models outside of Django. I would like some users to be able to query certain tables in the database using the powerful Django structure (querysets etc...) without giving full access to all the tables. Would any of the following strategies work? Is it even possible to do that? And what are the best practices for this kind of issues? Idea 1