Assume I am writing an app to change configurations in a machine. I have 3 created tables as below. Machine configuration shows the current state of configurations for our machine. Users can create their tickets and request for changes of the configurations. RequestDetails will be the table to keep the proposed cofigurations plus some extra info such as the name of the requestor, date etc.
These classes are just a simple examples, in the real model I would have nearly 600+ fields=configuration presented in class MachineConfiguration. I should have EXACTLY THE SAME fields in RequestDetails class too. I was wondering there is a way NOT TO REPEAT MYSELF when defining class RequestDetails when it comes to all the fields that exist in MachineConfiguration model?
I want to write it in a way that if I changed anything in MachineConfiguration table, the same change would apply to RequestDetails table too.
Thanks in advance for the help.
class RequestTicket(models.Model):
subject=models.CharField(max_length=50, null=False, blank=False)
description=models.TextField(null=False, blank=True)
class MachineConfiguration(models.Model):
field_1 = models.CharField(null=False,blank=True)
field_2 = models.CharField(null=False, blank=True)
field_3 = models.CharField(null=False, blank=True)
class RequestDetails(models.Model):
tracking_number=models.ForeignKey('RequestTicket')
field_A=models.DateField(null=True, blank=False)
field_B=models.TextField(null=True, blank=False)
field_1 = models.CharField(null=False, blank=True)
field_2 = models.CharField(null=False, blank=True)
field_3 = models.CharField(null=False, blank=True)
Yes you can create the base class and inherit that class in another class like,
class BaseModel(models.Model):
field1 = models.CharField()
field2 = models.CharField()
class Meta:
abstract = True
And inherit this class in another model to get those same field,
# Now if you change any field in BaseModel, it will reflect in both the models
class MachineConfiguration(BaseModel):
pass
class RequestDetails(BaseModel):
field3 = models.CharField()
Related
I'm having an issue with the table names format of my Django project.
Here is a sample of a model I have:
class WalletHistory(models.Model):
wallet = models.ForeignKey(Wallet, on_delete=models.CASCADE, related_name='wallet_history')
free_amount = AmountField(default=None, blank=True, null=True)
locked_amount = AmountField(default=None, blank=True, null=True)
flexible_amount = AmountField(default=None, blank=True, null=True)
date_created = models.DateTimeField(auto_now_add=True, blank=True)
The app associated with this model is called core so the table name is supposed to be core_wallet_history. But Django names my table core_wallethistory instead: it doesn't split the class names with underscore.
I've changed nothing in the Django settings.
Sorry I can't post comments yet as I don't have enough reputation.
How are rendering your tables?
I use django-tables2 and all I have to change is the attributes via the class Meta under the table.
Example:
class offer_table(ExportMixin,tables.Table):
offer_name = tables.Column(accessor='offer_name')
class Meta:
attrs = {"name":"offer_table", "id":"offer_table",}
Let me start by stating that I have looked at django-polymorphic for this and still have questions. I have item models and many subtypes for items. Currently, my models look like this.
class Item(models.Model):
pass
class Television(Item):
upc = models.CharField(max_length=12, null=True)
title = models.CharField(max_length=255, null=True)
brand = models.ForeignKey(Brand)
screen_size = models.IntegerField()
class Fridge(Item):
upc = models.CharField(max_length=12, null=True)
title = models.CharField(max_length=255, null=True)
brand = models.ForeignKey(Brand)
stuff_about_fridge = models.CharField(max_length=255, null=True)
I did this at first because then I wouldn't worry about all of the left joins when querying different item types that would be caused if my models looked like this:
class Item(models.Model):
upc = models.CharField(max_length=12, null=True)
title = models.CharField(max_length=255, null=True)
brand = models.ForeignKey(Brand)
class Television(Item):
screen_size = models.IntegerField()
class Fridge(Item):
stuff_about_fridge = models.CharField(max_length=255, null=True)
I am now reaching a point where I realize that I very often query all of the Item models together and have to left join information from the subtypes instead, so I am not really saving myself there. My question is, even if I used something like django-polymorphic, would it make sense to A) put everything that is shared in the parent model and just specific things in the child models or to B) have it like I do where everything is in the child model, but they share a parent model PK just so that they can be queried together?
"Abstract base classes are useful when you want to put some common information into a number of other models. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class"
Reference: https://docs.djangoproject.com/en/3.0/topics/db/models/#abstract-base-classes
The point of using inheritance is for sharing common info as mentioned in option (A).
Django-polymorphic makes using inherited models easier, nothing more.
Reference: https://django-polymorphic.readthedocs.io/en/stable/
That means, in conclusion, option (A) is correct even though if you use Django-polymorphic.
Below method is the right approach:
class Item(models.Model):
upc = models.CharField(max_length=12, null=True)
title = models.CharField(max_length=255, null=True)
brand = models.ForeignKey(Brand)
class Television(Item):
screen_size = models.IntegerField()
class Fridge(Item):
stuff_about_fridge = models.CharField(max_length=255, null=True)
I am working with some legacy database models in a Django REST Framework application:
class Variable(models.Model):
var_id = models.AutoField(primary_key=True)
resource_type = models.CharField(max_length=1, blank=True, null=True)
resource_id = models.BigIntegerField(blank=True, null=True)
var_name = models.CharField(max_length=500, blank=True, null=True)
class Meta:
managed = False
db_table = 'variables'
class Project(models.Model):
project_id = models.AutoField(primary_key=True)
name = models.CharField(unique=True, max_length=100, blank=True, null=True)
class Meta:
managed = True
db_table = 'projects'
Project and Variable are related models such that when the resource_type of a Variable is 'P', then its resource_id represents the project_id of the Project it belongs to. If the resource_type is something other than 'P, then that Variable belongs to a different type of model. I unfortunately can not make significant changes to the database schema for these models.
Is there a way to define a custom relationship between these two models so that I can treat them as if Variable was defined with a ForeignKey to Project? Or as if Project has a ManyToManyField relationship to Variable? I'd ultimately like to be able to create a nested serializer relationship. Something like:
class Variable(serializers.ModelSerializer):
class Meta:
model = models.Variable
fields = ('var_id', 'resource_type', 'resource_id', 'var_name')
class ProjectSerializer(serializers.ModelSerializer):
variables = VariableSerializer(many=True)
class Meta:
model = models.Project
fields = ('project_id', 'name', 'variables')
Thanks!
I have an abstract model that all my other models inherit from, it looks like this.
class SupremeModel(models.Model):
creator = models.ForeignKey(User, related_name="%(class)s_creator")
created = models.DateTimeField(null=True, blank=True)
deleted = models.BooleanField(default=False)
modified = models.DateTimeField(null=True,blank=True)
class Meta:
abstract = True
I then have a bunch of other models that inherit from this model, with something along these lines...
class ExampleModel(SupremeModel):
name = models.TextField(null=False, blank=False)
description = models.TextField(null=False, blank=False)
class AnotherModel(SupremeModel):
title = models.TextField(null=False, blank=False)
location = models.TextField(null=False, blank=False)
I want to create a Django model form for nearly all of my custom models that look similar to ExampleModel, but I always want the fields in SupremeModel to be excluded in the form...
How can I create a ModelForm that can be used to inherit the exclude parameters that will hide creator,created,deleted, and modified but show all of the other fields (in this case name and description or title and location).
you may try this
class ExcludedModelForm(ModelForm):
class Meta:
exclude = ['creator', 'created', 'deleted', 'modified']
class ExampleModelForm(ExcludedModelForm):
class Meta(ExcludedModelForm.Meta):
model = ExampleModel
I have some unmanaged models to get data from an Oracle database, but I cannot read anything in the documentation about joining tables.
I have these models
class Model1(models.Model):
id = models.CharField(max_length=200, primary_key=True)
name = models.CharField(max_length=200, blank=True, null=True)
class Meta:
managed = False
db_table = 'table_1_name'
class Model2(models.Model):
model1_id = models.CharField(max_length=200, blank=True, null=True)
class Meta:
managed = False
db_table = 'table_2_name'
and I'm trying to join the tables in model 1 and model 2.
I thought I could use model1 = models.ForeignKey(Model1) in Model2 to get data with obj.model1__name, but I cannot make it work.
The problem isn't anything to do with unmanaged models - it's just a question of syntax.
The ForeignKey declaration is correct, but accessing the related information is done via the dot syntax: obj.model1.name.