将数据保存在一对一模型Django中

时间:2017-05-06 13:03:04

标签: python django

我有两个不同的Django应用程序。 地理位置 2.列表

当用户或管理员创建新的列表时,我需要更新地理位置模型。

到目前为止,这是我的geolocations / models.py

from django.db import models
from apps.listings.models import Listing
from geopy.geocoders import Nominatim


# Create your models here.
class ListingGEOLocations(models.Model):
    class Meta:
        # Set DB name
        db_table = 'livelocal_listing_geo_location'
        # Admin backend listing name
        verbose_name = 'Listing GEO Location'
        verbose_name_plural = 'Listings GEO Locations'

    geo_location = models.CharField(
        max_length=100,
        db_column='geo_location',
        unique=True,
        blank=False,
        null=False,
        verbose_name='Geo Location'
    )
    listing = models.OneToOneField(
        Listing,
        db_column='listing',
        blank=False,
        unique=True
    )

    def save(self, *args, **kwargs):
        geolocator = Nominatim()
    pass

    def __str__(self):
        return self.geo_location

在我的listing / models.py

   from django.db import models
    import uuid
    from django.template.defaultfilters import slugify

class ListingRootCategory(models.Model):
    class Meta:
        # Set DB name
        db_table = 'local_root_category'
        # Admin backend listing name
        verbose_name = 'Root Category'
        verbose_name_plural = 'Root Categories'

    root_category = models.CharField(
        max_length=100,
        db_column='root_category',
        unique=True,
        blank=False,
        null=False
    )

    slug = models.SlugField(
        blank=True,
        db_column='slug',
        null=False,
        unique=True,
        verbose_name='Slug'
    )

    def save(self):
        self.slug = slugify(self.root_category)
        super(ListingRootCategory, self).save()

    def __str__(self):
        return self.root_category


class ListingSubCategory(models.Model):
    class Meta:
        # Set DB name
        db_table = 'local_sub_category'
        # Admin backend listing name
        verbose_name = 'Sub Category'
        verbose_name_plural = 'Sub Categories'

    # Use ForeignKey : map to ListingRootCategory model
    root_category = models.ForeignKey(
        ListingRootCategory
    )
    sub_category = models.CharField(
        max_length=100,
        db_column='sub_category',
        unique=True,
        blank=False,
        null=False,
        verbose_name='Sub Category'
    )
    slug = models.SlugField(
        blank=True,
        db_column='slug',
        null=False,
        unique=True,
        verbose_name='Slug'
    )

    def save(self):
        self.slug = slugify(self.sub_category)
        super(ListingSubCategory, self).save()

    def __str__(self):
        return self.sub_category


class Listing(models.Model):
    class Meta:
        # Set DB name
        db_table = 'local_listing'
        # Admin backend listing name
        verbose_name = 'Listing'
        verbose_name_plural = 'All Listings'

    # Use ForeignKey : map to ListingRootCategory model
    main_category = models.ForeignKey(
        ListingRootCategory,
        db_column='main_category',
        verbose_name='Main Category'
    )
    # Use ForeignKey : map to ListingSubCategory model
    sub_category = models.ManyToManyField(
        ListingSubCategory,
        db_column='sub_category',
        verbose_name='Sub Category'
    )

    # Use UUIDField to generate unique ID for each business
    account_number = models.UUIDField(
        max_length=100,
        db_column='account_number',
        blank=False,
        null=False,
        unique=True,
        default=uuid.uuid4,
        verbose_name='Account Number',
        editable=False
    )
    name = models.CharField(
        max_length=250,
        db_column='name',
        blank=False,
        null=False,
        verbose_name='Business Name'
    )
    address1 = models.CharField(
        max_length=250,
        db_column='address1',
        blank=False,
        null=False,
        verbose_name='Address1'
    )
    address2 = models.CharField(
        max_length=250,
        db_column='address2',
        blank=True,
        null=True,
        verbose_name='address2'
    )
    phone = models.CharField(
        max_length=50,
        db_column='phone',
        blank=False,
        null=False,
        verbose_name='Phone'
    )
    fax = models.CharField(
        max_length=50,
        db_column='fax',
        blank=True,
        null=True,
        verbose_name='Fax'
    )
    email = models.EmailField(
        max_length=200,
        db_column='email',
        blank=False,
        null=False,
        verbose_name='Email'
    )
    website_url = models.CharField(
        max_length=100,
        db_column='website_url',
        blank=True,
        null=True,
        verbose_name='Website'
    )
    opening_hours = models.CharField(
        max_length=50,
        db_column='opening_hours',
        blank=False,
        null=False,
        verbose_name='Opening hours'
    )
    # True/False choice
    YES_NO = (
        ('True', 'Yes'),
        ('False', 'No'),
    )
    description = models.TextField(
        max_length=500,
        db_column='description',
        blank=False,
        null=False,
        verbose_name='Description',
        default=''
    )
    # To make sure if the business is open
    status = models.CharField(
        max_length=10,
        choices=YES_NO,
        blank=False,
        null=False,
        db_column='status',
        verbose_name="Business is permanently close?"
    )
    # To make sure if the business listing is claimed
    claimed = models.CharField(
        max_length=10,
        choices=YES_NO,
        blank=False,
        null=False,
        db_column='claimed',
        verbose_name="Business page is claimed?"
    )
    lastupdate = models.DateTimeField(
        auto_now=True,
        db_column='last_update_date',
        verbose_name='Last Update Date'
    )
    slug = models.SlugField(
        unique=True
    )

    def save(self):
        super(Listing, self).save()
        self.slug = '%s' % (
            slugify(self.name)
        )
        super(Listing, self).save()

    def __str__(self):
        return self.name

我想要实现的是当管理员或用户创建新的列表(通过表单)时,我想创建GEOPY生成并将该地址保存在该特定实例的ListingGEOLocations表中。

0 个答案:

没有答案