将json数据插入MySQL数据库

时间:2022-09-27 16:18:57

1.I want to save data from database(mysql) to .json file format.

1。我想将数据从数据库(mysql)保存到.json文件格式。

Models.py

Models.py

class Author(models.Model):
    author_id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    age=models.IntegerField()

    class Meta:
    db_table=u'Author'

    def __unicode__(self):
        return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age)



class Book(models.Model):
    book_id=models.AutoField(primary_key=True,unique=True)
    book_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    author=models.ForeignKey(Author)

    class Meta:
        db_table = u'Book'

    def __unicode__(self):
        return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name)

views.py is

的观点。py是

def addbook(request):

    log.debug("test....")
    if request.POST:

        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        email = request.POST.get('email')
        age = request.POST.get('age')

    author = Author(first_name = first_name,last_name = last_name,email=email,age=age)
        author.save()

        book_name = request.POST.get('book_name')
        publisher_name = request.POST.get('publisher_name')
        author_info = Author.objects.latest('author_id')

        log.debug("test:%s",author_info.author_id)

    book=Book(book_name=book_name,publisher_name=publisher_name,author_id=author_info.author_id)
    book.save()
    return redirect('/index/')
    else:
        return render_to_response('addbook.html',context_instance=RequestContext(request))

1) In views.py an addbook function is used to add the related data to database.

1)在视图。使用addbook函数将相关数据添加到数据库中。

2) I have to store the database content to a json file after each entry.

2)每次输入后,我必须将数据库内容存储到json文件中。

3) Can i get help to code the same.

我能得到同样代码的帮助吗?

1 个解决方案

#1


0  

This link explains how to serialize Django models. You will have to add something like this to your model code:

这个链接解释了如何序列化Django模型。您必须在模型代码中添加如下内容:

from django.core import serializers

class Book(models.Model):
    ...
    def serialize(self): 
        JSONSerializer = serializers.get_serializer("json")
        json_serializer = JSONSerializer()  
        with open("/path/to/file/file.json", "w") as out:
            json_serializer.serialize([self], stream=out)

The square brackets around [self] are important if you want to serialize a particular instance of a model. If you are serializing a queryset then you can pass that in instead.

如果要序列化模型的特定实例,[self]周围的方括号是很重要的。如果您正在序列化一个queryset,那么您可以将其传入。

#1


0  

This link explains how to serialize Django models. You will have to add something like this to your model code:

这个链接解释了如何序列化Django模型。您必须在模型代码中添加如下内容:

from django.core import serializers

class Book(models.Model):
    ...
    def serialize(self): 
        JSONSerializer = serializers.get_serializer("json")
        json_serializer = JSONSerializer()  
        with open("/path/to/file/file.json", "w") as out:
            json_serializer.serialize([self], stream=out)

The square brackets around [self] are important if you want to serialize a particular instance of a model. If you are serializing a queryset then you can pass that in instead.

如果要序列化模型的特定实例,[self]周围的方括号是很重要的。如果您正在序列化一个queryset,那么您可以将其传入。