Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:45:41+00:00 2026-06-13T13:45:41+00:00

I have following model (campaigns/models.py) from django.db import models from django.contrib.auth.models import User class

  • 0

I have following model (campaigns/models.py)

from django.db import models
from django.contrib.auth.models import User

class Module(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class TestType(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class Test(models.Model):
    PROTO_CHOICES = (
        ('TCP','tcp'),
        ('UDP','udp'),
    )
    module = models.ForeignKey(Module)
    testtype = models.ForeignKey(TestType)
    name = models.CharField(max_length=50, unique=True)
    port = models.IntegerField(blank=True, null=True)
    proto = models.CharField(max_length=3, blank=True, choices=PROTO_CHOICES)
    payload = models.TextField()
    sig_match = models.TextField(blank=True)
    def __unicode__(self):
        return self.name

class Campaign(models.Model):
    name = models.CharField(max_length=50, unique=True)
    tests = models.ManyToManyField(Test)
    updated = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, editable=False)
    def __unicode__(self):
        return self.name

class CampaignRun(models.Model):
    campaign = models.ForeignKey(Campaign)
    name = models.CharField(max_length=50, unique=True)
    run_start = models.DateTimeField()
    run_end = models.DateTimeField()
    updated = models.DateTimeField(auto_now=True)
    user = models.ForeignKey(User, editable=False)
    class Meta:
    def __unicode__(self):
        return self.name

class TestRun(models.Model):
    campaignrun = models.ForeignKey(CampaignRun)
    test = models.ForeignKey(Test)
    test_start = models.DateTimeField()
    test_end = models.DateTimeField()
    alert = models.TextField(blank=True)
    flag = models.IntegerField(blank=True, null=True)

…which resulted in following database (sqlite3):

                                                                       +-----------------------------+
                                                                       |                             |
   +---------------------------+     +------------------------------+  |     +--------------------+  |
   | campaigns_testrun         |     | campaigns_test               |  |     | campaigns_module   |  |
   +----------------+----------+     +----------------+-------------+  |     +------+-------------+  |
   | id             | INT      |  +--| id             | INT         |--+  +--| id   | INT         |  |
+--| campaignrun_id | INT      |  |  | module_id      | INT         |-----+  | name | VARCHAR(20) |  |
|  | test_id        | INT      |--+  | testtype_id    | INT         |--+     +------+-------------+  |
|  | test_start     | DATETIME |     | name           | VARCHAR(50) |  |                             |
|  | test_end       | DATETIME |     | port           | INT         |  |     +--------------------+  |
|  | alert          | TEXT     |     | proto          | VARCHAR(3)  |  |     | campaigns_testtype |  |
|  | flag           | INT      |     | payload        | TEXT        |  |     +------+-------------+  |
|  +----------------+----------+     | sig_match      | TEXT        |  +-----| id   | INT         |  |
|                                    +----------------+-------------+        | name | VARCHAR(20) |  |
|                                                                            +------+-------------+  |
|                                                                                                    |
|  +------------------------+                                +--------------------------+            |
|  | campaigns_campaignrun  |     +--------------------+     | campaigns_campaign_tests |            |
|  +-------------+----------+     | campaigns_campaign |     +-------------+------------+            |
+--| id          | INT      |     +------+-------------+     | id          | INT        |            |
   | campaign_id | INT      |-----| id   | INT         |-----| campaign_id | INT        |            |
   | name        | INT      |     | name | VARCHAR(50) |     | test_id     | INT        |------------+
   | run_start   | DATETIME |     +------+-------------+     +-------------+------------+
   | run_end     | DATETIME |     
   +-------------+----------+     

Here is the logic:

  • A campaign is composed of tests
  • A campaign can be run many times (campaignrun) and results in test results (test run).
  • For each campaign run, tests related information are gathered from the test table

Obviously, there is a circular relationship and the integrity of the database is only ensured by the application.

However, from a database architect perspective, this circular relation is incorrect since it can results in integrity problems:

  • In SQL directly or from the django admin module, it is possible to create tests in campaigns_testrun that are NOT in the campaign_tests table

For the above reason, the solution would be to link campaigns_testrun with campaigns_campaign_tests directly. However, I don’t think it is possible in Django.

Should you have any idea how to deal with this, please be welcomed to comment.

Thank you in advance for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:45:42+00:00Added an answer on June 13, 2026 at 1:45 pm

    Use ManyToManyField through argument.

    This allows you to use a custom model, and refer to it in FKs, M2M, etc …

    Also note that you can quote your class names, ie. ForeignKey(‘SomeClass’), enabling circular relations without problem.

    Welcome to Django, enjoy your ride B)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model. from django.db import models class Client(models.Model): postcode = models.CharField(max_length=10)
I have the following model: class User: name = models.CharField( max_length = 200 )
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following Model: class Group(models.Model): member = models.ManyToManyField(Player, through='GroupMember') name = models.CharField(max_length=20,
I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height =
I have the following model: class mark(models.Model): title=models.CharField(max_length=35) url=models.URLField(max_length=200) user=models.ManyToManyField(User,blank=True) and then I use
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model: class Day(models.Model): day = models.DateField() title = models.CharField(max_length=20) description
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have following model. class Comment(models.Model): type = models.CharField(max_length=21, choices=OBJECT_TYPE_CHOICES) program = models.ForeignKey(Program, db_column='object_id',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.