Questions tagged [django-model-field]

112 questions
0
votes
1 answer

How can i update the field of a model from another model field in django?

I have two models. DepartmentGoal with a field 'scores' and Task also with a field 'scores' and a foreign departmentgoal. What I want is; If I allocate scores to DepartmentGoal, scores allocated to Task should subtract from scores of DepartmentGoal…
0
votes
1 answer

view images uploaded from admin on html in django

I am learning to write a Django app which will fetch all images from os.path.join(BASE_DIR, 'media_root', 'uploads') and show it on html page. But its not working like so. admin.py from .models import Experience # Register your models…
nish
  • 1,008
  • 4
  • 17
  • 34
0
votes
0 answers

django models, NOT NULL constraint failed on foreignKey, even tough i put null=True in my model class

I implemented the user model and authentication first and added some users to the database. Then i added two more fields to the models, "city" and "Company", and set foreignKey relations among them. It's ok if a user i create through admin site has…
0
votes
1 answer

Django get model datefield value

class Bla(Case): def __init__(self, model, field, condition=None, then=None, **lookups): la = model._meta.get_field(field).__str__ returns > How do I get the value of…
24thDan
  • 113
  • 1
  • 9
0
votes
1 answer

How to use a Bootstrap 5 class (form-check) with Django 3.2.2?

I am learning Django and I want to use the form-check Bootstrap class for a checkbox but it isn't working correctly. This is the code of my file vaccines_form.html: {{ form.vacuna }} This code of my file forms.py works fine: widgets = { …
0
votes
0 answers

django signals does not save m2m model field

// signals.py @receiver(pre_save, sender=AcademicLesson) def take_attendance(sender, instance, **kwargs): new_file = instance.class_img if new_file: sub = instance.sub_id # this for loops gives all student from each…
0
votes
1 answer

django uuid or hashid field for primary keys? and how to prefix the generated ids with say "cust_"

I am currently using django-shortuuidfield to generate a unique UUID primary key on the Customer model as shown below class Customer(models.Model): customer_id = ShortUUIDField() user = models.ForeignKey(User, on_delete=models.CASCADE) …
Abishek
  • 369
  • 4
  • 21
0
votes
1 answer

Create django model with as many fields as an integer in other field

I have the following model in django class params(models.Model): name = models.CharField(max_length=30, default = 'no_name') cs_n = models.IntegerField(default=16) alt_n = models.IntegerField(default=2) opt_out =…
Daniel Pérez
  • 51
  • 1
  • 2
0
votes
1 answer

How to use on_delete CASCADE for some objects and ondelete DO_NOTHING for others in djando models

I have a Django model that refers to itself as below: parent = models.ForeignKey('self', ...) I need to act in some situations as on_delete=models.CASCADE and in other situations as on_delete=models.DO_NOTHING. How can I do that?
0
votes
1 answer

I need help in choosing relevant field for my django models , want a field like a list which can contain multiple items

I'm working on a project where a user can add an event, and another user can enroll themself in that event. I want to add a user's name to the list whenever they enroll in the event. My model: class Event(models.Model): Topic = CharField …
0
votes
1 answer

Django reverse (on_delete) protection on a model instance

Is is possible to protect a model in a reverse relationship. For instance in the models below:- class Foo(models.Model): foo_field1 = models.CharField(max_length=56, unique=True) class Bar(models.Model): bar_field1 = models.ForeignKey(Foo,…
Asif
  • 23
  • 6
0
votes
1 answer

Creating a custom function in model to compare two foreign key max timestamps

In my Django project, I am attempting to create a custom model field that relies on comparing the values of two reverse foreign key timestamps. In this toy example, I operate some car dealerships. Cars are hired out to folks but need to be…
William
  • 171
  • 1
  • 12
0
votes
0 answers

Django - size of ArrayField based on a field value of a related model

I have 2 models: Report and STO with a one-to-many relationship between them, so a STO can have many related reports. STO model class STO(TerminableWithTerminableRelatedModel): name = models.CharField(_('name'), unique=True, blank=False,…
dc_Bita98
  • 851
  • 2
  • 17
  • 35
0
votes
1 answer

Is django model field lookup_type 'contains' case-sensitive or insensitive..?

As per documentation 'contains' field lookup is case-sensitive and 'icontains' is case-Insensitive, but I don't see any difference while i'm querying it. >>> from users.models import SnetUser >>>…
0
votes
1 answer

Is there a way to pass the value of a model field as a parameter to models.ImageField(storage=function) within same Model

class StorageModel(models.Model): user = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="profile", null=False) file_meta = models.ImageField(storage=UserDataStorage(profile=user), blank=False, null=False) class…