Django模型设计(大模型与多重)

时间:2017-12-31 09:12:38

标签: sql django database-design django-models

我对设计模型有疑问。假设我有以下模型:

Exception in thread "main" org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'sysadmin-ThinkPad-L440', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-30-generic', java.version: '1.8.0_151'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {appActivity: nu.lower.brightness.SplashS..., appPackage: nu.lower.brightness, browserName: android, databaseEnabled: false, desired: {appActivity: nu.lower.brightness.SplashS..., appPackage: nu.lower.brightness, browserName: android, deviceName: MauryaJi, newCommandTimeout: 120, noReset: true, platform: ANDROID, platformName: Android, udid: 93ffd01a0803, version: 7.0 NRD90M}, deviceManufacturer: Xiaomi, deviceModel: Redmi Note 4, deviceName: 93ffd01a0803, deviceScreenSize: 1080x1920, deviceUDID: 93ffd01a0803, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, newCommandTimeout: 120, noReset: true, platform: ANDROID, platformName: ANDROID, platformVersion: 7.0, takesScreenshot: true, udid: 93ffd01a0803, version: 7.0 NRD90M, warnings: {}, webStorageEnabled: false}
Session ID: 45824a17-fa92-4570-b5e4-a09c578b3367
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at io.appium.java_client.PerformsTouchActions.performTouchAction(PerformsTouchActions.java:41)
    at io.appium.java_client.TouchAction.perform(TouchAction.java:325)
    at com.appium.test.TestCase1.brightnessControl(TestCase1.java:125)
    at com.appium.test.ExecuteTest.main(ExecuteTest.java:11)``

现在我想要多个主题的模型(ShoppingList,Games,...)。我找到了两种可能的解决方案,需要帮助来决定更合适的解决方案。

1)为每个想要的新模型制作注释摘要并扩展它。

class Comment(models.Model):
    who = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    text = models.CharField(max_length=1000)
    likes = models.IntegerField(default=0)
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, related_name='child_comments')

然后我可以用以下内容查询这个游戏评论:

class ShoppingListComment(Comment): shopping_list = models.ForeignKey(ShoppingList, related_name='shopping_comments', on_delete=models.CASCADE)

2)直接添加额外可空的Foreing键进行评论:

ShoppingListComment.objects.all()

然后我可以用lile:

来查询这个游戏评论
class BigCommentModel(models.Model):
    who = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    text = models.CharField(max_length=1000)
    likes = models.IntegerField(default=0)
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, related_name='child_comments')
    shopping_list = models.ForeignKey(ShoppingList, related_name='shopping_comments', on_delete=models.CASCADE, null=True),
    game = models.ForeignKey(Game, related_name='game_comments', on_delete=models.CASCADE, null=True)

0 个答案:

没有答案