Home > Software engineering >  Django access information model with foreign key
Django access information model with foreign key

Time:02-26

my models.py

class Information(models.Model):
    id = models.CharField(max_length=200, primary_key=True)
    title = models.CharField(max_length=500)
    link = models.CharField(max_length=100)
    summary = models.CharField(max_length=1000)
    published = models.CharField(max_length = 100)

    def __str__(self):
        return self.title

class Search(models.Model):
    id = models.CharField(max_length=100, primary_key=True)
    searched_titles = models.CharField(max_length=100)
    searched_topics = models.CharField(max_length=100)
    number_found_articles = models.IntegerField()

    def __str__(self):
        return self.id

class Article_search(models.Model):
    found_articles = models.ForeignKey(
        Information, on_delete=models.CASCADE, default=None, primary_key=True)
    search_details = models.ForeignKey(
        Search, on_delete=models.CASCADE)

my views.py

def show_articles_with_this_filter_id(request):
    alles = Article_search.objects.all()
    print(alles)

the error i get: (1054, "1054 (42S22): Unknown column 'database_article_search.found_articles_id' in 'field list'", '42S22')

if i make my models.py Article_search class like this:

class Article_search(models.Model):
    found_articles = models.ForeignKey(
        Information, on_delete=models.CASCADE, default=None)
    search_details = models.ForeignKey(
        Search, on_delete=models.CASCADE)

I get this error: (1054, "1054 (42S22): Unknown column 'database_article_search.id' in 'field list'", '42S22')

I know every models needs a primary key, but i don't understand why all give an error. Please can someone help me to access the data from this model

After suggestions i made the models.py file like:

class Information(models.Model):
    id = models.CharField(max_length=200, primary_key=True)
    title = models.CharField(max_length=500)
    link = models.CharField(max_length=100)
    summary = models.CharField(max_length=1000)
    published = models.CharField(max_length = 100)

    def __str__(self):
        return self.title

class Search(models.Model):
    id = models.CharField(max_length=100, primary_key=True)
    searched_titles = models.CharField(max_length=100)
    searched_topics = models.CharField(max_length=100)
    number_found_articles = models.IntegerField()

    def __str__(self):
        return self.id

class Article_search(models.Model):
    found_articles = models.OneToOneField(
        Information,
        on_delete=models.CASCADE,
        primary_key=True,)
    search_details = models.OneToOneField(
        Search, on_delete=models.CASCADE)

when i try to fetch all information from the Article_search table with views.py i get the error: (1054, "1054 (42S22): Unknown column 'database_article_search.found_articles_id' in 'field list'", '42S22')

CodePudding user response:

A foreign key can never be a primary key for other models, to achieve that you have to use OneToOne field instead of the foreign key.

  • Related