Django fails to migrate | ValueError: Cannot serialize: <Price: 0.99 €> - django

I am trying to migrate some changes on a Model but it doesn't apply the migration because of this Model:
class Price(models.Model):
price = models.FloatField(default=0.00)
type = models.CharField(max_length=100, blank=True)
def __str__(self):
return f'{self.price} €'
Error:
ValueError: Cannot serialize: <Price: 0.99 €>
There are some values Django cannot serialize into migration files.
Migration file:
Migrations for 'core':
core/migrations/0001_initial.py
- Create model Album
- Create model Genre
- Create model Language
- Create model Price
- Create model Services
- Create model Track
- Create model Songwriter
- Create model Artist
- Add field artist to album
- Add field language to album
- Add field price to album
- Add field primary_genre to album
- Add field secondary_genre to album
- Add field services to album
- Add field user to album
- Create model Account

Related

How can I model a database view in Django if it doesn't have a primary key?

I have a database view that relates 2 companies by foreign keys like so:
DB company_view:
company1_id FK to Company,
company2_id FK to Company,
description text
where
--- some company criteria ---
I try model in Django as unmanaged like so:
class CompanyView(models.Model):
company1 = models.ForeignKey(Company, related_name='company1_id', parent_link=True)
company2 = models.ForeignKey(Company, related_name='company2_id', parent_link=True)
description = models.TextField()
class Meta:
managed = False
db_table = 'company_view'
For the Admin class I have:
#admin.register(models.CompanyView)
class CompanyViewAdmin(AdvancedModelAdmin):
list_display = ('company1', 'company2', 'description')
But the admin page throws exception like:
psycopg2.errors.UndefinedColumn: column company_view.id does not exist
It doesn't make sense to have a primary id key, so is there any way around this?
Thanks
why you dont make a many-to-many relationship?
company = models.ManyToManyField(Company)
look: https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_many/
this will solve your problem, since it use a object instance.

How to show a related_name field from Django models connected through a ManyToMany relationship?

New to Django and relational DBs. I'm building the classic Doctor appointment booking app and have come to a point where I don't know what to do. I've created the Doctor model pointing to a Clinic, but in my API the Clinic model won't show a list of all Doctors. How could I achieve this?
class Clinic(models.Model):
name = models.CharField(max_length=200)
class Doctor(models.Model):
clinic = models.ManyToManyField(Clinic, related_name="doctors")
class ClinicSerializer(serializers.ModelSerializer):
class Meta:
model = Clinic
fields = '__all__'
class DoctorSerializer(serializers.ModelSerializer):
class Meta:
model = Doctor
fields = '__all__'
class ClinicViewset(viewsets.ModelViewSet):
queryset = Clinic.objects.all()
serializer_class = ClinicSerializer
class DoctorViewset(viewsets.ModelViewSet):
queryset = Doctor.objects.all()
serializer_class = DoctorSerializer
Django will automatically create the link from Clinic to Doctor. You don't need to (shouldn't) define it. From the docs:
Django also creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all().
The related_name argument that you passed to ManyToManyField when you created the clinic field is the name of the relation from Clinic to Doctor. It is optional and if you didn't pass it, it would be the lower-cased named of the model + _set - doctor_set in your case.
Usually you would set it to a plural in the case of ManyToManyField. In your case: doctors.
Because you have related_name="doctor" currently, you can retrieve a clinic's doctors with: clinic.doctor.all().

Django model foreign key not installed or abstract

im creating 2 models in django, the first one has django auth user as a foreign key, the second has this first model as a foreign key like this in models.py :
class SGIUsers(models.Model):
charge = models.CharField('Cargo', max_length=80)
user = models.ForeignKey(User, unique=True)
class ResponsibleStateFlow(ModelBase):
user = models.ForeignKey(SGIUsers, verbose_name = 'Responsable', blank = False )
process= models.ForeignKey(Process, verbose_name='Proceso')
But i get this error :
sgiprocess.ResponsibleStateFlow.user: (fields.E300) Field defines a relation with model 'SGIUsers', which is either not installed, or is abstract.
I already imported django auth user of course. Any idea ??
try to add the app name:
field=models.ForeignKey('app_name.ModelName')
Found it, i needed to define an app label with the app name in class meta for SGIUsers like this first:
class Meta:
app_label = 'app_name'
And then call the foreign with 'app_name.Modelname'

writing a model for existing mssql table Django

I've this tables in a mssql server:
Table name: Items
Fields:
ItemID - primarykey - integer
StorageLocation - varchar(100)
Table name: Inventory
Fields:
InvetoryID - primarykey - integer
ItemID - foreignkey - integer
sku - integer
QtyToList - integer
How can I tell to Django that ItemID is the ID field and it's also the PK ? (do I need to tell it also it's an integer field?)
What is the best way to model it in django.
Thanks in advance
Amit
class Item(models.Model):
class Meta:
db_table='Items'
item_id = models.IntegerField(primary_key=True, db_column='ItemID')
storage_location = models.CharField(max_length=100, db_column='StorageLocation')
class Inventory(models.Model):
class Meta:
db_table='Inventory'
inventory_id = models.IntegerField(primary_key=True, db_column='InventoryID')
item = models.ForeginKey('Item', db_column='ItemID')
sku = models.IntegerField()
qty_to_list = models.IntegerField(db_column='QtyToList')
Model Field Reference: https://docs.djangoproject.com/en/1.7/ref/models/fields/#db-column
Model Meta Reference: https://docs.djangoproject.com/en/1.7/ref/models/options/
First, I recommend using the 'inspectdb' command, then you can modified the result as you want. Please visit the inspectdb documentation for more details on it.
The documentation is quite helpful.

Django :Suggestion for django model

I am using django models to create my database .
here is my User Class
class User(models.Model):
user_name = models.CharField(max_length=100,null = False)
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
tags = here what to do ?
now i am planning to add tags for User so that user can select the tags (it can be more then one also )
Here is my Tags Class
Class Tags(models.Model)
tag = models.CharField()
my question is which relation should i use for the reference Tags from User table (Foreign key or Manytomany )
Note: In future i will search the users based on tags so please suggest me the better way to do this
Use a ManytoMany relationship: different users may use the same tags and a single User will have several tags:
tags= models.ManyToManyField(Tags, verbose_name="list of tags")
Anyway, you don't have to implement a Model for the User, there is a User model that comes with django. See the docs:
Django docs
Django book