django-models

How perform patch operation on nested serializer with dictionary field

☆樱花仙子☆ 提交于 2020-01-24 12:18:49
问题 I have Model with dictionary object, in below example want to set and get daily availability example Currently I am able to read, want to make this to read and write, what should I do for this "teacher_date": [ { "day_available": "MON", "time_available": "Morning" }, { "day_available": "SAT", "time_available": "Afternoon" }, { "day_available": "SUN", "time_available": "Evening" } Here is my model.py class Availability(models.Model): uid = models.AutoField(verbose_name='ID', serialize=False,

Prevent Django from updating identity column in MSSQL

怎甘沉沦 提交于 2020-01-24 06:45:10
问题 I'm working with a legacy DB in MSSQL. We have a table that has two columns that are causing me problems: class Emp(models.Model): empid = models.IntegerField(_("Unique ID"), unique=True, db_column=u'EMPID') ssn = models.CharField(_("Social security number"), max_length=10, primary_key=True, db_column=u'SSN') # Field name made lowercase. So the table has the ssn column as primary key and the relevant part of the SQL-update code generated by django is this: UPDATE [EMP] SET [EMPID] = 399, ....

Django polymorphic models with unique_together

半世苍凉 提交于 2020-01-24 03:02:08
问题 Lets say I have these this base model: class Trackable(PolymorphicModel): uuid = UUIDField(unique=True) created_by = models.ForeignKey(settings.AUTH_USER_MODEL) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) And a child model extends it: class Like(Trackable): content = models.ForeignKey(Content, related_name='likes') class Meta: unique_together = ['content', 'created_by'] When I run migration, it complains about: django.db.models.fields

How to make a login view for django custom user?

青春壹個敷衍的年華 提交于 2020-01-24 00:50:09
问题 I have made a custom user model using the AbstractUser , removed the username and replaced it with email and extended the model, tried creating superuser and it worked and also created some users by a signup form , logged in the admin interface and it worked however when tried to create a login form for the users it fails I tried this but it didn't work def LoginView(request): if request.method == 'POST': form = AuthenticationForm(data=request.POST) if form.is_valid(): user = form.get_user()

Giving a model knowledge of a many-to-many related model in django

筅森魡賤 提交于 2020-01-23 16:54:24
问题 EDIT: Given responses in comments and answer I tried suggestion and I get some errors when trying to query , also doing the related name query does not get the right results (as seen in comments) BusinessLocations.objects.all() Error: QuerySet object has no attribute 'objects' is the error. In either case, I did a dump of all the tables and see this: auth_business_permissions', u'auth_permission', u'auth_user', u'auth_user_businesss', u'auth_user_user_permissions', u'django_admin_log', u

django: calculate percentage based on object count

巧了我就是萌 提交于 2020-01-23 12:53:47
问题 I have the following models: class Question(models.Model): question = models.CharField(max_length=100) class Option(models.Model): question = models.ForeignKey(Question) value = models.CharField(max_length=200) class Answer(models.Model): option = models.ForeignKey(Option) Each Question has Options defined by the User. For Example: Question - What is the best fruit? Options - Apple, Orange, Grapes. Now other user's can Answer the question with their responses restricted to Options . I have

Display image from ImageField by means of form

十年热恋 提交于 2020-01-23 09:58:17
问题 prelude : Here's the simpliest way to display an ImageField. Lets assume I have 10 fields in my form and I don't wanna iterate over all of them in template.html just to check if it's an imageField and display it in a different way. I want to handle it via form or widget or something like this. So I want the leave template.html as it's bellow. template.html <table> {{ form.as_table }} </table> And add to models.py an image_tag method: class UserProfile(Model): photo = ImageField(upload_to

Django Model is saved, but returns None

 ̄綄美尐妖づ 提交于 2020-01-22 20:50:17
问题 I have a simple model with a Model Manager: class CompanyReviewManager(models.Manager): def get_votes_for_company(self, company): try: return CompanyReview.objects.filter(user = user).count() except ObjectDoesNotExist: return None def get_rating_for_field(self, installer, field): try: return CompanyReview.objects.filter(user = user).aggregate(Avg(field)) except ObjectDoesNotExist: return None class CompanyReview(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL) satisfaction =

Django Model is saved, but returns None

こ雲淡風輕ζ 提交于 2020-01-22 20:50:07
问题 I have a simple model with a Model Manager: class CompanyReviewManager(models.Manager): def get_votes_for_company(self, company): try: return CompanyReview.objects.filter(user = user).count() except ObjectDoesNotExist: return None def get_rating_for_field(self, installer, field): try: return CompanyReview.objects.filter(user = user).aggregate(Avg(field)) except ObjectDoesNotExist: return None class CompanyReview(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL) satisfaction =

Query for top x elements in Django

若如初见. 提交于 2020-01-22 17:03:26
问题 I have two models such that class JobTitle(models.Model): name = models.CharField(max_length=1000) class Employer(models.Model): jobtitle = models.ForeignKey(JobTitle,unique=False,null=True) As you see, one employer may have many jobtitles. I try to make a query to get top 5 employers whose number of job titles is maximum How can I achive this is Django ? Thanks 回答1: Employer.objects.values('id').annotate(jobtitle_count=Count('jobtitle')).order_by('-jobtitle_count')[:5] 回答2: from django.db