I'm new to Django and trying to create a small application that shows scanned data from virtual machines that are inserted in a table named HostsFixDataScans.
To access the scanned data in HostsFixDataScans via the Hosts model, I defined a ForeignKey with to_field.
But unfortunately, the data returned by the linked HostsFixDataScans are wrong.
I checked the SQL statements and when requesting the HostsFixDataScans table, not the id of the Host is used, but always 1.
My domain = models.ForeignKey(Domains, on_delete=models.CASCADE) definition which does not use to_field works correctly.
I'm pretty sure, I have a misunderstanding of the definition of this relationship.
Maybe you could give me some hints how to solve the problem?
Many thanks in advance!
Here are the shortened definitions of the models:
class Os(models.Model):
operatingsystem = models.CharField(max_length=32)
lsbdistcodename = models.CharField(max_length=32)
lsbdistrelease = models.CharField(max_length=32, db_collation='ascii_bin')
lsbmajdistrelease = models.IntegerField()
remarks = models.CharField(max_length=256, blank=True, null=True)
class HostsFixDataScans(models.Model):
host_id = models.PositiveIntegerField(primary_key=True, unique=True)
scan_id = models.PositiveIntegerField()
os = models.ForeignKey(Os, on_delete=models.CASCADE)
class Hosts(models.Model):
hostname = models.CharField(max_length=256)
domain = models.ForeignKey(Domains, on_delete=models.CASCADE)
is_virtual = models.PositiveIntegerField(blank=True, null=True)
os = models.ForeignKey(HostsFixDataScans, to_field='host_id', on_delete=models.CASCADE)
Sorry folks,
I made a really stupid mistake by connecting the field os_id with the field host_id on the other table.
I now added a field last_fix_data_scan_id to the Hosts table which has to be filled with every scan of a machine now.
But doing it this way I can now request the Hosts by connecting this new field with the scan_id of HostsFixDataScans.
Related
I want to copy data and add it to another field without lose or corrupt data. after that delete old field and continue with new field. Im using postgresql and when i try this connection with fields to copy data i get e.300 and e.307 error as i see on internet this problem cause by wrong foreign key usage and caused by same problem.
POST MODEL
class Post(models.Model):
user = models.ForeignKey('user.CustomUser', verbose_name='Author', on_delete=models.CASCADE, related_name='posts')
title = models.CharField(max_length=120)
content = models.TextField()
image = models.ImageField(null=True, blank=True, upload_to="media/images")
publishing_date = models.DateTimeField(verbose_name='Publishing Date', auto_now_add=True)
COMMENT MODEL
class Comment(models.Model):
post = models.ForeignKey('post.Post', related_name='comments', on_delete=models.CASCADE)
name = models.CharField(max_length=200, verbose_name='Name')
# user = models.ForeignKey('self.user', related_name='comment', on_delete=models.CASCADE)
content = models.TextField(verbose_name='Comment')
created_date = models.DateTimeField(auto_now_add=True)
I'm currently trying to copy name field to user field and when makemigrate i get e.307 and e.300 errors
ERRORS:
post.Comment.user: (fields.E300) Field defines a relation with model 'self.user', which is either not installed, or is abstract.
post.Comment.user:(fields.E307) The field post.Comment.user was declared with a lazy reference to 'self.user', but app 'self' isn't installed.
Thanks for anyone who try to help.
NOTE I am not necessarily asking for code to build this, just ideas on how to do this. Links and blog posts for pointers are welcome.
I am building a rest api.
I have a model
class Showcase(models.Model):
title = models.CharField(max_length=50)
description = models.TextField(null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name="Showcases")
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=255, unique=True)
def __str__(self):
return self.title
I am trying to build a functionality where the user that created a showcase can add users that contributed to the project which is the showcase. I was thinking of making this its own model like this:
class Collaborator(models.Model):
post = models.ForeignKey(Showcase, on_delete=models.CASCADE, related_name="collaborated_showcases")
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name="showcase_owner")
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name="collaborators")
skill = models.ForeignKey(Skill, on_delete=models.CASCADE, null=True, related_name="creative_type")
role = models.TextField(null=True)
added_on = models.DateTimeField(null=True)
def __str__(self):
return f"{self.user.name} collaborated on {self.post.name}"
The user would have to search for users and then add them as a contributor to the showcase, which is where my brain scrambles a bit.
The other important thing is that I want to be able to randomly go to a user and get ALL the showcases he has contributed to.
As I see it, this structure works fine for your use case, though:
models should always be in singular case (Collaborator)
related_names should be lower case (related_name="showcases")
and I prefer to explicitly spell out all related_names, so I'd add
Collaborator.post related name collaborated_showcases
Collaborator.user related name collaborators
Showcase.user related_name owned_showcases
Then,
To find an user's owned showcases, Showcase.objects.filter(user=user)
To find an user's collaborated showcases, Showcase.objects.filter(collaborators=user) (I think)
I'd suggest having a Collaborator object for the owner user as well, so you can show their role more easily as well as make these queries simpler.
I am currently working on a Django 1.5.2 project within a Docker instance that speaks with a mysql database in a separate Docker instance. I am trying to create a Many to Many relationship between two tables by creating a middle table that contains two foreign keys that point to the two tables that need connecting. The problem arises when I run python manage.py syncdb and it spits out the following error to the terminal: NameError: name 'QueryString' is not defined. QueryString is clearly defined in my models.
Here are my Models...
class Tag(models.Model):
name = models.CharField(max_length=100)
class QueryStringTab(models.Model):
tag = models.ForeignKey(Tag, related_name='querystringtab')
querystring = models.ForeignKey(QueryString, related_name='querystringtab')
class QueryString(BaseObject):
"""
Query string holds an SQL statement and query properties for execution
"""
server_id = models.IntegerField()
schema = models.CharField(max_length=255, blank=True)
query = models.CharField(max_length=60000)
variables = models.TextField(blank=True)
created_by = models.ForeignKey(User, related_name='queries_created')
updated_by = models.ForeignKey(User, related_name='queries_last_edited')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField()
touched_by = models.CharField(max_length=1000)
config = models.TextField(blank=True)
runs_started = models.IntegerField(default=0)
runs_completed = models.IntegerField(default=0)
runs_completed_duration = models.IntegerField(default=0) # total number of seconds spent running this query to completion
formats = "pretty_html html json prettyjson csv excel tableau".split()
Noteworthy points...
1) It is recognizing the Tag model just fine.
2) Could it have something to do with the fact that QueryString is a BaseObject
3) It is successfully creating the Tag table in the mysql database
Can anyone find anything obvious that I am doing wrong?
The declaration of QueryStringTab is before the one for QueryStringTab; so when Python evaluates the first, it has not yet seen any definition for the second and therefore reports a NameError.
Django allows you to use a string target than a class object in cases like this:
querystring = models.ForeignKey('QueryString', related_name='querystringtab')
Or, you could simply move the definition of QueryStringTab to the end.
I am using Django REST Framework. I have two models, Sites and Statuses.
class Sites(models.Model):
site_id = models.AutoField(primary_key=True)
status = models.ForeignKey(Statuses, models.DO_NOTHING, blank=True, null=True)
class Statuses(models.Model):
status_id = models.AutoField(primary_key=True)
description = models.CharField(max_length=255, blank=True, null=True, unique=True)
class Meta:
managed = True
db_table = 'Statuses'
I would like to be able to perform a GET on sites, and have the Statuses.description field returned (instead of Statuses.status_id). Also, I would like it so that either status_id or description may be used interchangeably in a POST to create a new site. Where does this type of functionality belong (serializer, models, etc...)?
I know I can accomplish the first part of my question by adding a property to the Sites model and then referencing this field in the Sites serializer.
#property
def status(self):
return self.row_status.description
However I thought the convention of a Model is that it should be a 1:1 representation of the database table. Is there a better way to do this?
This fits well in the serializer, like this:
class SitesSerializer(serializers.ModelSerializer):
description = serializers.CharField(source='status.description')
class Meta:
model = Sites
fields = ('site_id', 'description')
(But the status field should probably not have null=True set.)
I´m using Django to import from an existing mysql database thats already created and is used by somebody else. Im using:
python manage.py inspectdb > models.py
And the class from my model in question is the following:
class Funciones(models.Model):
idfuncion = models.IntegerField(primary_key=True)
idpelicula = models.ForeignKey(Peliculas, db_column='idpelicula')
idcine = models.ForeignKey(Cines, db_column='idcine')
hora = models.TextField() # This field type is a guess.
tipo_3d = models.IntegerField(null=True, db_column=u'3d', blank=True) # Field renamed
xd = models.IntegerField(null=True, blank=True)
gtmax = models.IntegerField(null=True, blank=True)
vip = models.IntegerField(null=True, blank=True)
idioma = models.CharField(max_length=33)
idciudad = models.ForeignKey(Ciudades, db_column='idciudad')
class Meta:
db_table = u'funciones'
Now this attribute:
hora = models.TextField() # This field type is a guess
corresponds to a time dataType in MYSQL.
Now, since I cant change the dataType in MYSQL because somebody else has already created a number of queries to the database from another app, I would like to know what would be the appropriate corresponding django datatype that I should use in my django model.
I was thinking DateTimeField but then I should have to somehow truncate the date part before writing to the database.
Any ideas?
Thanks
Edit:
As pointed out by Pedro:
Django actually has a TimeField, why not use that? – Pedro Romano
You could use the Timedelta object to represent "time elapsed" using this https://djangosnippets.org/snippets/1060/