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 6089729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:07:59+00:00 2026-05-23T12:07:59+00:00

I have two models like so: class Visit(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=65535,

  • 0

I have two models like so:

class Visit(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=65535, null=False)

class Session:
    id = models.AutoField(primary_key=True)
    visit = models.ForeignKey(Visit)
    sequence_no = models.IntegerField(null = False)

I’d like two write a custom create method in the Session model so when I write this:

visitor = Vistor.objects.get(id=1)
new_session = Session.objects.create_new_session(visit=visitor)

…I get a new record in the Session table with the next consecutive sequence number for that visitor i.e. 3. Here’s some sample data

VISITOR SEQUENCE_NO
------- -----------
1         1
1         2
2         1
2         2
1         3 (This would be the row that would be created)

The other was writing a custom get method in the Session model so that I write:

visitor = Vistor.objects.get(id=1)
new_session = Session.objects.get_session(visit=visitor, sequence_no=3)

…I get the previous record of that visitor with the highest sequence number. Here’s some sample data

VISITOR SEQUENCE_NO
------- -----------
1         1
1         2 (This would be the row that would be fetched)
2         1
2         2
1         3 

Could you tell me how to accomplish this please? Should this code be in the Model or the Manager?

Thanks everyone.

  • 1 1 Answer
  • 1 View
  • 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-05-23T12:08:00+00:00Added an answer on May 23, 2026 at 12:08 pm

    This would be in a Manager for Sessions. It would look something like (untested):

    class SessionManager(models.Manager):
        def new_session(self, visit, **kwargs):
            vs = self.model.objects.filter(visit=visit).order_by("-sequence_no")[:1]
            if len(vs):
                kwargs.update({"sequence_no": vs.sequence_no + 1})
            return Super(SessionManager, self).create(visit=visit, **kwargs)
    

    As for getting a session when you have visit, and sequence_no there shouldn’t be any custom code needed for that.

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

Sidebar

Related Questions

If I have two models like class Author(models.Model): name = models.CharField(max_length=100) title = models.CharField(max_length=3,
I have two models like this: class Store(models.Model): name = models.CharField(max_length=255) class Order(models.Model): store
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
Let's say I have two models looking like this: class ProductType(models.Model): product_type = models.CharField(max_length=100)
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I have two models like this: class AA(models.Model): name = models.CharField() state = models.IngerField()
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have two tables like so: class Collection(models.Model): name = models.CharField() class Image(models.Model): name
I have two models: call them questions and answers: class FacetQuestion(models.Model): the_question = models.CharField(max_length=50)

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.