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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:01:09+00:00 2026-05-28T19:01:09+00:00

I imported my (PHP) old site’s database tables into Django. By default it created

  • 0

I imported my (PHP) old site’s database tables into Django. By default it created a bunch of primary key fields within the model (since most of them were called things like news_id instead of id).

I just renamed all the primary keys to id and removed the fields from the model. The problem then came specifically with my News model. New stuff that I add doesn’t appear in the admin. When I remove the following line from my ModelAdmin, they show up:

list_display = ['headline_text', 'news_category', 'date_posted', 'is_sticky']

Specifically, it’s the news_category field that causes problems. If I remove it from that list then I see my new objects. Now, when I edit those items directly (hacking the URL with the item ID) they have a valid category, likewise in the database. Here’s the model definitions:

class NewsCategory(models.Model):

    def __unicode__(self):
        return self.cat_name

    #news_category_id = models.IntegerField(primary_key=True, editable=False)
    cat_name = models.CharField('Category name', max_length=75)
    cat_link = models.SlugField('Category name URL slug', max_length=75, blank=True, help_text='Used in URLs, eg spb.com/news/this-is-the-url-slug/ - generated automatically by default')

    class Meta:
        db_table = u'news_categories'
        ordering = ["cat_name"]
        verbose_name_plural = "News categories"

class News(models.Model):

    def __unicode__(self):
        return self.headline_text

    #news_id = models.IntegerField(primary_key=True, editable=False)
    news_category = models.ForeignKey('NewsCategory')
    writer = models.ForeignKey(Writer) # todo - automate
    headline_text = models.CharField(max_length=75)
    headline_link = models.SlugField('Headline URL slug', max_length=75, blank=True, help_text='Used in URLs, eg spb.com/news/this-is-the-url-slug/ - generated automatically by default')
    body = models.TextField()
    extra = models.TextField(blank=True)
    date_posted = models.DateTimeField(auto_now_add=True)
    is_sticky = models.BooleanField('Is this story featured on the homepage?', blank=True)
    tags = TaggableManager(blank=True)

    class Meta:
        db_table = u'news'
        verbose_name_plural = "News"

You can see where I’ve commented out the autogenerated primary key fields.

It seems like somehow Django thinks my new items don’t have news_category_ids, but they definitely do. I tried editing an existing piece of news and changing the category and it worked as normal. If I run a search for one of the new items, it doesn’t show up, but the bottom of the search says “1 News found”, so something is going on.

Any tips gratefully received.

EDIT: here’s my ModelAdmin too:

class NewsCategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {"cat_link": ("cat_name",)}
    list_display = ['cat_name', '_cat_count']

    def _cat_count(self, obj):
        return obj.news_set.count()
    _cat_count.short_description = "Number of news stories"      

class NewsImageInline(admin.TabularInline):
    model = NewsImage
    extra = 1    

class NewsAdmin(admin.ModelAdmin):
    prepopulated_fields = {"headline_link": ("headline_text",)}
    list_display = ['headline_text', 'news_category', 'date_posted', 'is_sticky'] #breaking line
    list_filter = ['news_category', 'date_posted', 'is_sticky']
    search_fields = ['headline_text']
    inlines = [NewsImageInline]
  • 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-05-28T19:01:10+00:00Added an answer on May 28, 2026 at 7:01 pm

    The answer you are looking for I think would lie in the SQL schema that you altered and not in the django models.

    It could probably have something to do with null or blank values in the news_category_id, or news that belongs to a category that doesn’t exist in the news_category. Things I’d check:

    • You have renamed the primary key on the News category from news_category_id to id. Does the foreign key on the News also map to news_category_id and not anything else?
    • Are all the values captured in the news.news_category also present in news_category.id

    Also, as an aside, I don’t see any reason why you need to rename the primary keys to id from something that they already are. Just marking them primary_key=True works just fine. Django provides you a convenient alias pk to access a model’s integer primary key, irrespective of what the name of the field actually is.

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

Sidebar

Related Questions

I have just imported a huge MySQL database. Most fields are latin1_swedish_ci, and they
I'm new to Django and am still trying to break old PHP habits. Below
In my site there have an installation part.In that the database is imported(the datbase
I imported a series of blogger posts (via xml) into WordPress, and the YouTube
I imported LuaInterface into a console project, referenced it, and wrote a small test
I imported system.web into my class lib project. Trying to find: system.web.httpserverutility.server.mappath But I
I have imported an animated GIF as a resource into a picturebox - unfortunitly
I've just imported a Flex component into my project. I have a theory question
I just imported my Wordpress database from a localhost installation on my computer to
I am trying to pull data from a table I have imported into my

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.